How to preallocate memory if I don't know the size of the variable in every iteration?

3 views (last 30 days)
I have a large set of data, say A. I have a for loop. In every iteration, I'm going to only work with the subset of data of A, say A_sub, which are the elments of A that satisfy some conditions, such as within some ranges. Then, I'm also going to work with the subset of the A_sub, say A_sub_sub, which are the elements of A_sub that satisfy certain conditions, such as within some ranges. I don't know how many elements that's going to satisfy the certain condition, so I don't know the size of A_sub and A_sub_sub in advance. So the size of A_sub and A_sub_sub is going to change at each iteration. Is there a better way to do it?
  1 Comment
Walter Roberson
Walter Roberson on 8 Jun 2019
You do not need to pre-allocate A_sub or A_sub_sub in those conditions, as you would be writing over all of them.
You would want pre-allocation if you were saving the results into a variable. In such a case, the best way to do pre-allocation would depend on how you want the saved information to be arranged relative to the other saved information.

Sign in to comment.

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!