As an appendix to gariepy's answer:
The matrix concatenation
A = [A k]; as a way of appending to it is actually pretty slow. You end up reassigning N elements every time you concatenate to an N size vector. If all you're doing is adding elements to the end of it, it is better to use the following syntax
A(end+1) = k; In MATLAB this is optimized such that on average you only need to reassign about 80% of the elements in a matrix. This might not seam much, but for 10k elements this adds up to ~ an order of magnitude of difference in time (at least for me).

Bare in mind that this works only in MATLAB 2012b and higher as described in this thead: Octave/Matlab: Adding new elements to a vectorOctave/Matlab: Adding new elements to a vector
This is the code I used. tic/toc syntax is not the most accurate method for profiling in MATLAB, but it illustrates the point.
close all; clear all; clc; t_cnc = []; t_app = []; N = 1000; for n = 1:N; % Concatenate tic; A = []; for k = 1:n; A = [A k]; end t_cnc(end+1) = toc; % Append tic; A = []; for k = 1:n; A(end+1) = k; end t_app(end+1) = toc; end t_cnc = t_cnc*1000; t_app = t_app*1000; % Convert to ms % Fit a straight line on a log scale P1 = polyfit(log(1:N),log(t_cnc),1); P_cnc = @(x) exp(P1(2)).*x.^P1(1); P2 = polyfit(log(1:N),log(t_app),1); P_app = @(x) exp(P2(2)).*x.^P2(1); % Plot and save loglog(1:N,t_cnc,'.',1:N,P_cnc(1:N),'k--',... 1:N,t_app,'.',1:N,P_app(1:N),'k--'); grid on; xlabel('log(N)'); ylabel('log(Elapsed time / ms)'); title('Concatenate vs. Append in MATLAB 2014b'); legend('A = [A k]',['O(N^{',num2str(P1(1)),'})'],... 'A(end+1) = k',['O(N^{',num2str(P2(1)),'})'],... 'Location','northwest'); saveas(gcf,'Cnc_vs_App_test.png');