Possible Duplicate:
Growable data structure in MATLAB
So in my current MATLAB script, I have a very large indeterminate-sized growing array. There is currently nothing I can do about it, because if I actually preallocate, it would take many many many times more memory than it should need (maximum possible amount of values is 640 per pixel, but usually it’s something along the lines of 2-5).
Usually in this case, I’d be using a vector in C++ or something, where it grows exponentially in relation to a given capacity. But I think the matrices in Matlab starts fragmenting much faster than the purpose driven C++ vectors.
What do you guys think is the best alternative to something like this? Or should I just stick with normal arrays and hope that adding around 100k elements sequentially will work?
Thanks in advance.
You could try what
std::vectordoes when reallocating elements — double its capacity every time it fills up which has an amortized cost of O(1).Test:
Output:
Using cell