Use the following
A = [A elem] % for row array
or
A = [A; elem] % for col array
Edit: Another simpler way is (as @BenVoigt suggested) to use end keyword
A(end+1) = elem;
which works for both row and column vectors.
Another way is
A(end+1) = elem;
which works for both row and column vectors.
Videos
.append() equivalent in MATLAB
Appending strings to array
How do I properly append to an array within a loop?
How to append arrays of different lengths
I need to grow an array, matrix, or cell array or table one element at a time by appending. I want to make sure I'm using a data structure in Matlab that has fast appending, so that I'm not slogging through O(n) on each append operation. (Also, I can prepend if that is somehow faster?) I do not know the number of rows I will be adding ahead of time, so I cannot pre-allocate the structure. I must instead grow it as new data comes in.
What data structure should I use for this to ensure speed and efficiency?
Attempts to google this issue return strange rumors, such as this is faster to do with Cell arrays rather than a matrices. Rumors that using "end" is an "order of magnitude faster" and other unsubstantiated forum posts.
I'm doing a largish machine learning vision task and I have over 5000 training images. I need to extract features from them, and each image outputs a random number of them ranging anywhere from 100 to 400 or so. Also, I may have other people in the lab who train on a different dataset, possibly larger than mine. So the code had to be robust to size this data structure on-the-fly depending on what is coming in, image-by-image.
your thoughts?