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

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-6-8
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by