Save in a cell array from a loop

For every iterration of my loop I have a 2D matrix (A), but the number of rows is different every time.
Ideally, in the end Iwould like to have a 3D matrix [(A)which is 2D x number of iterrations]. As I can't save in matrix format,
how can I save the results in a cell array? and how can I access the data fro a specific itteration?
ndays=365;
for d=1:ndays
help=[Temperature(:,d),Idx(:,d)];
idx=Idx(:,d);
tst=find(idx==6);
take=help(tst,:);
%save test in a 3D matrix [takex ndays];
end

4 Comments

Do you want to save take? Or maybe tst? Either way, you can create a cell array
C = cell(ndays,1);
before the loop and store whatever you want in C{d} at each iteration of the loop.
For example:
ndays=365;
C = cell(ndays,1);
for d=1:ndays
help=[Temperature(:,d),Idx(:,d)];
idx=Idx(:,d);
tst=find(idx==6);
take=help(tst,:);
%save test in a 3D matrix [takex ndays];
C{d} = take;
end
To access the data, you use the same syntax used to store the data. To get the data from the 5th iteration, for example, you can use
data = C{5}
However, running your code on the .mat files you provided gave me an error after 10 iterations because Temperature and Idx only have 10 columns instead of 365 columns.
kounoupaki87
kounoupaki87 on 7 Apr 2020
Edited: kounoupaki87 on 7 Apr 2020
Yes, because it was to big to upload it all:) Thank you for your help:)
Ah that makes sense - happy to help!
@Tommy, your answer helped me too. Thanks!

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 7 Apr 2020

Edited:

on 15 Apr 2021

Community Treasure Hunt

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

Start Hunting!