+1 for an interesting question.
EDITED Answer: From a small pilot study at first it seems that it is better to add a line later, but now he seems to be more effective and can be reassigned again, when you have some information about the right size. I started with a matrix size of 3000 and proposed a 10% error in the assessment of the size, see below.:
clear all clc guess_size=3000; m=zeros(guess_size); %1. oops overesrimated, take out rows tic m(end-300:end,:)=[]; toc %1b. oops overesrimated, preallocate again tic m=zeros(guess_size-300,guess_size); toc %2. oops overesrimated, take out cols m=zeros(guess_size); tic m(:,end-300:end)=[]; toc %2b. oops overesrimated, preallocate again m=zeros(guess_size); tic m=zeros(guess_size,guess_size-300); toc %3. oops underesrimated, add rows m=zeros(guess_size); tic m=zeros(guess_size+300,guess_size); toc %4. oops underesrimated, add cols m=zeros(guess_size); tic m=zeros(guess_size,guess_size+300); toc Elapsed time is 0.041893 seconds. Elapsed time is 0.026925 seconds. Elapsed time is 0.041818 seconds. Elapsed time is 0.023425 seconds. Elapsed time is 0.027523 seconds. Elapsed time is 0.029509 seconds. ; clear all clc guess_size=3000; m=zeros(guess_size); %1. oops overesrimated, take out rows tic m(end-300:end,:)=[]; toc %1b. oops overesrimated, preallocate again tic m=zeros(guess_size-300,guess_size); toc %2. oops overesrimated, take out cols m=zeros(guess_size); tic m(:,end-300:end)=[]; toc %2b. oops overesrimated, preallocate again m=zeros(guess_size); tic m=zeros(guess_size,guess_size-300); toc %3. oops underesrimated, add rows m=zeros(guess_size); tic m=zeros(guess_size+300,guess_size); toc %4. oops underesrimated, add cols m=zeros(guess_size); tic m=zeros(guess_size,guess_size+300); toc Elapsed time is 0.041893 seconds. Elapsed time is 0.026925 seconds. Elapsed time is 0.041818 seconds. Elapsed time is 0.023425 seconds. Elapsed time is 0.027523 seconds. Elapsed time is 0.029509 seconds.
Options 2b and 1b is slightly faster than an underestimation, so if you can, it is better to overestimate, and then pre-redirect. Never delete rows from the array. Furthermore, the addition of columns appears somewhat more effective, but it's just a quick and dirty job. See. @Shai detailed response to the inner workings of ...
source share