How to construct an array with varying dimension?

조회 수: 4 (최근 30일)
ramin bba
ramin bba 2014년 6월 30일
댓글: ramin bba 2014년 6월 30일
I want to save some data using a "for loop". Within each iteration, I will get a 2D matrix (which I want to save). the size of the matrices is NOT the same, i.e. first 10*10, then 23*23, next 20*20 and so on (no pattern between the sizes). I clearly cannot use a 3D matrix to save the data since the third dimension is not constant. I cannot save them manually/separately either since there is a lot of 2D matrices (this is actually why i am putting it in a for loop). I also do NOT know the size of the largest 2D matrix.
what can I use to do the above? cell? structure?
tnx in advance

채택된 답변

Image Analyst
Image Analyst 2014년 6월 30일
Yes. Use a cell array. That's what it's meant for. See the FAQ for a good explanation and some good illustrative examples: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 6월 30일
A cell array would work well for this problem. Since you have a for loop, you have an idea of how many matrices you will need to save. Try the following:
% size the cell array where n is the number of iterations in the for loop (and
% the number of matrices to save)
cellArray = cell(n,1);
% iterate
for k=1:n
% do stuff
% save kth matrix A to cell array
cellArray{k} = A;
end
And that is it - all matrices will be saved to the cellArray object.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by