How to store the output of multiple for loops in an empty matrix in a particular order?

조회 수: 1 (최근 30일)
Hi everyone,
My script consists of 2 loops with multiple conditions. For each iteration, I get an output that consists of 3 rows and 4 columns. I want to save this output in an empty matrix in sequential order. May someone suggest how I can do this?
Here is my attempt
T=[]
for kk=1:2
s2=t_d(t_d(:,1)>= CE_L(kk) & t_d(:,1)<= CE_M(kk),:);
s1=v_d(v_d(:,1)>= CE_L(kk) & v_d(:,1)<= CE_M(kk),:);
for i=0:1:3;
t1_f= addtodate(CE_L(kk), i, 'day');
% multiple missing step to make the probelm simple
ww=1:length(tzcd);
ph=abs(phz);
phase=mean(ph);
med=median(ph);
st=std(ph);
obb=i;
res=[i phase med st];
TT=[TT,res];
end
end
However this save outpout as an array, whereas i need as a matrix. My output is looks like this
0.0 166.1 167.0 2.3
1.0 169.5 166.4 11.0
2.0 160.8 162.9 5.7
3.0 157.6 151.7 21.1
for each iteration of kk loop, where as each row represent the output of i iteration.

채택된 답변

Alex Hanes
Alex Hanes 2022년 10월 26일
편집: Alex Hanes 2022년 10월 26일
After each iteration of your for loop over i (for i=0:1:3 ... ), you replace the array, TT, that stores everything sequentially. Consider pre-allocating an array for TT and update after each loop, as you are already doing:
nk = 2; % number of iterations in your kk-loop
ni = 4; % number of iterations in your i-loop
nc = 4; % number of columns from your 'res' each loop
TT = zeros(nk*ni,nc); % pre-allocate
Now, instead of having TT = [TT,res]; in your code, replace it with:
for kk = 1:nk
for ii = 0:1:ni-1
% Stuff
idx = (kk-1)*ni + ii + 1; % calculate index
TT(idx,:) = res;
end
end
  댓글 수: 3
Alex Hanes
Alex Hanes 2022년 10월 26일
I updated my original response just now. linInd should have been idx.
Andi
Andi 2022년 10월 26일
편집: Andi 2022년 10월 26일
@Alex Hanes thank you! but it still only stored results from last iteration.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by