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

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.

 Accepted Answer

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 Comments

@Alex Hanes thanks for help, but still there is this error
Unrecognized function or variable 'linInd'.
Error in untitled7 (line 113)
TT(linInd,:) = res;
Here is how i modified the script
clear all
clc
% BELOW SCRIPT IS FOR MULTIPLE DAYS CALCULATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cd_ev=readmatrix('T_time.csv'); %
ev_time=datenum(cd_ev(:,1),cd_ev(:,2),cd_ev(:,3),cd_ev(:,4),cd_ev(:,5),cd_ev(:,6));
CE_M=ev_time';
for jj=1:29
b=CE_M(:,jj);
aa(jj)= addtodate(b, 5, 'day');
bb(jj)= addtodate(b, -5, 'day');
end
CE_U=aa;
CE_L=bb;
% % ------------ Data Set 2: -------------%
s_tid=dlmread('TID.csv', ',', 1, 1);
t1=datenum(s_tid(:,1),s_tid(:,2),s_tid(:,3),s_tid(:,4),s_tid(:,5),s_tid(:,6));
t_d=[t1 s_tid(:,7)];
%------------- Data Set 3: --------------%
s_ven=dlmread('VENT.csv', ',', 1, 1);
ts=datenum(s_ven(:,1),s_ven(:,2),s_ven(:,3),s_ven(:,4),s_ven(:,5),s_ven(:,6));
v_d=[ts s_ven(:,7)];
% ---------- Data selection for a particular event ---------%
%tt=[];
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
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),:);
ser1=s1(:, 2)-mean(s1(:,2)); % series 1 minus its mean
ser2=s2(:, 2)-mean(s2(:,2)); % series 2 minus its mean
ta=s1(:, 1);
ta1=(ta-ta(1));
tb=s2(:, 1);
tb1=(tb-tb(1));
% figure(1);
% subplot(211);
% plot(ta1,ser1,'-b');
% legend('Vent Fluid')
% title('Vent Fluid Temp'); grid on
% subplot(212);
% plot(tb1,ser2,'r-');
% title('Tidal Height'); grid on
% xlabel('Time (days)')
% legend('Tidal')
% obb=zeros(1,20); %allocate array for phase angles
% phase=zeros(1,20); %allocate array for estimates of dominant frequency
for i=0:1:3;
t1_f= addtodate(CE_L(kk), i, 'day');
t2_f= addtodate(CE_L(kk), i+2, 'day');
sf2=t_d(t_d(:,1)>= t1_f & t_d(:,1)<= t2_f,:);
sf1=v_d(v_d(:,1)>= t1_f & v_d(:,1)<= t2_f,:);
if length(sf1) ~= length(sf2)
continue;
end
S=[sf1 sf2];
tfa=sf1(:, 1);
tfa=(tfa-tfa(1)); %time (days) (initial offset removed)
sff1=S(:, 2)-mean(S(:,2)); % series 1 minus its mean
sff2=S(:, 4)-mean(S(:,4)); % series 2 minus its mean
N=length(tfa);
dt=(tfa(end)-tfa(1))/(N-1); %sampling interval (days)
fs=1/dt; fNyq=fs/2; %sampling frequency, Nyquist frequency (cycles/day)
f1=1; f2=4; %bandpass corner frequencies (cycles/day)
[z,p,k]=butter(4,[f1,f2]/fNyq,'bandpass'); %4th order Butterworth bandpass filter coefficients
[sos,g]=zp2sos(z,p,k); %sos representation of the filter
y1=filtfilt(sos,g,sff1); %apply zero phase filter to ser1
y2=filtfilt(sos,g,sff2); %apply zero phase filter to ser2
% % %--------- Zero crossing time --------%
%
zci = @(v) find(diff(sign(v))>0); %define function: returns indices of +ZCs
izc1=zci(y1); %find indices of + zero crossings of y1
izc2=zci(y2); %find indices of + zero crossings of y2
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZT1 = ZeroX(tfa(izc1),y1(izc1),tfa(izc1+1),y1(izc1+1));
ZT2 = ZeroX(tfa(izc2),y2(izc2),tfa(izc2+1),y2(izc2+1));
if length(ZT1)==length(ZT2)
tzcd=ZT1-ZT2; %zero crossing time differences
elseif length(ZT1)==length(ZT2)-1
tzcd=ZT1-ZT2(1:end-1); %zero crossing time differences
elseif length(ZT1)-1==length(ZT2)
tzcd=ZT1(2:end)-ZT2; %zero crossing time differences
end
fdom=(length(ZT2)-1)/(ZT2(end)-ZT2(1));
phz=360*fdom*tzcd; %phase lag of y1 relative to y2 (degrees)
ww=1:length(tzcd);
ph=abs(phz);
phase=mean(ph);
med=median(ph);
st=std(ph);
obb=i;
res=[i phase med st];
for kk = 1:nk
for ii = 0:1:ni-1
idx = (kk-1)*ni + ii + 1; % calculate index
TT(linInd,:) = res;
end
end
end
end
I updated my original response just now. linInd should have been idx.
@Alex Hanes thank you! but it still only stored results from last iteration.

Sign in to comment.

More Answers (0)

Products

Tags

Asked:

on 26 Oct 2022

Edited:

on 26 Oct 2022

Community Treasure Hunt

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

Start Hunting!