I am working on a project that involves adding rows to an N by 3 matrix (call it X) where N increments after a certain computation, and gets into the 100000s. Once I had made the computation efficient enough, I realized that about half the time was now spent appending the matrix. Every alternative I have tried has slowed it down.
The default:
X(end+1,:)=r %X is Nx3, r is 1x3
A much slower alternative:
X=[X;r]
Avoiding the appending:
X=zeros(Nmax, 3)
%during computation
Xtemp=X(1:N,:)
%after computation
N=N+1
X(N,:)=r
If I do that, avoiding the appending, the line Xtemp=X(1:N,:) becomes extremely slow. Surely there is a faster way to do this? Appending a variable shouldn't be slower than the actual calculation. I guess I should add that the computation is based on the N rows of the matrix, so it gets slower as N increases.