77

Having a vector x and I have to add an element (newElem) .

Is there any difference between -

x(end+1) = newElem; 

and

x = [x newElem]; 

?

3 Answers 3

94

x(end+1) = newElem is a bit more robust.

x = [x newElem] will only work if x is a row-vector, if it is a column vector x = [x; newElem] should be used. x(end+1) = newElem, however, works for both row- and column-vectors.

In general though, growing vectors should be avoided. If you do this a lot, it might bring your code down to a crawl. Think about it: growing an array involves allocating new space, copying everything over, adding the new element, and cleaning up the old mess...Quite a waste of time if you knew the correct size beforehand :)

Sign up to request clarification or add additional context in comments.

6 Comments

Also for the second method, x must be initialized first!
@RodyOldenhuis, no problem! I did the same, probably at the same time. @Dan, that's true, but as the question mentioned "having a vector x (of size n)", I kind of assumed n to be non-zero and the vector being initialized already :)
@ThijsW: Still, using end rather than some variable n (which might be a global for all you know!) is the more universal, robust-no-cost way to go
@Dan x must be initialised for both methods.
@Robino I don't think so, I don't have MATLAB to check but in octave you can certainly do x(end+1)=... without x being initialised and I'm almost certain you can do it in MATLAB as well
|
28

Just to add to @ThijsW's answer, there is a significant speed advantage to the first method over the concatenation method:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc Elapsed time is 0.004611 seconds. Elapsed time is 0.016448 seconds. Elapsed time is 0.034107 seconds. Elapsed time is 12.341434 seconds. 

I got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get

Elapsed time is 0.003044 seconds. Elapsed time is 0.009947 seconds. Elapsed time is 12.013875 seconds. Elapsed time is 12.165593 seconds. 

So I guess the speed advantage only applies to more recent versions of Matlab

5 Comments

+1, Edited to add the obvious as well. I'll test again on a "real" CPU (I'm on this crappy unreliable no-good APU thing now...)
@Dan, same for me, I get 0.028 for the 3rd option and 8.909 for the last
I also think the JIT optimisation for the x(end+1) case is a pretty recent addition (R2012a or so...). I vagualey remember reading something like that in some changenotes at smoe point. I also get very different results on my APU/Matlab R2010, but I'm not sure if that's due to the Matlab version or the APU...
@RodyOldenhuis and ThiijsW see my recent comparison between older and newer matlab
@Dan: Ah :) OK, NOW it's all better :)
4

As mentioned before, the use of x(end+1) = newElem has the advantage that it allows you to concatenate your vector with a scalar, regardless of whether your vector is transposed or not. Therefore it is more robust for adding scalars.

However, what should not be forgotten is that x = [x newElem] will also work when you try to add multiple elements at once. Furthermore, this generalizes a bit more naturally to the case where you want to concatenate matrices. M = [M M1 M2 M3]


All in all, if you want a solution that allows you to concatenate your existing vector x with newElem that may or may not be a scalar, this should do the trick:

 x(end+(1:numel(newElem)))=newElem 

4 Comments

I think your last example should be: x(end+1:end+length(newElem)) = newElem
@Digna Thanks for finding the bug, I have updated the answer to fix the problem.
With my Matlab2011b, there was also a drastic (~50x) speed improvement on vector concatenation with this method vs. the a=[a b] method.
@Jabe the [a b] method is concatenation. I suppose you are saying that extending the array is quicker than concatenation?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.