필터 지우기
필터 지우기

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