store vctors of non-fixed size

조회 수: 1 (최근 30일)
mingcheng nie
mingcheng nie 2022년 12월 28일
답변: Walter Roberson 2022년 12월 28일
I have a loop to call a function, this function will return a vector, the size of vector is not fixed. However, in the main function, I want to store the returned vector of each loop into a matrix or any other places. Is there any way to tackle this size changing issue? Seems that the preallocation is difficult.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 12월 28일
If you have a known number of iterations, then pre-allocate a cell array and store the results into that.
If you have a maximum possible return size per iteration, sometimes it makes sense to take the output, pad it with filler values to the maximum size, and store the padded value into a pre-allocated array.
If I am reading multiple files in which I do not know maximum reasonable size, then at each step I read in a file, and compare the dimensions to the current dimensions of my accumulated data. If the new data is smaller in a dimension than the accumulated data, then I pad the data to the current size. If the new data is larger in some dimension than the saved data then I pad the saved data to the new size. Then now that the sizes are compatible, store the new data. This code takes a bit of work (it is easier if padding with zero is acceptable.)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by