Complicated for loops structuring

조회 수: 1 (최근 30일)
Jonathan Cheong
Jonathan Cheong 2021년 3월 22일
답변: Jonathan Cheong 2021년 3월 23일
Hello, this is a question for the pros, and feel free to only answer if it interest you.
I have this code which helps me model data for each independent event.
% Event 1
sm1 = smcellere{1,:}; t1 = 1:length(sm1);SMF_1 = (1:0.3:SMO(1));good1 = sm1(~isnan(sm1)).';
figure(1)
plot(t1,sm1,'--ob');
hold on
for i = 1:length(SMF_1)
for ii = 1:length(tau)
% This is the model
SM1 = (SMO(1)-SMF_1(i))*exp(-t1./tau(ii))+SMF_1(i);
rmse1(i,ii) = sqrt(sum((SM1-good1).^2)/length(good1));
end
end
[iii,jjj] = find(rmse1 == min(rmse1(:)));
smf1=SMF_1(iii);tau1 = tau(jjj);
sm1calc = (SMO(1)-smf1)*exp(-t1./tau1)+smf1;
plot(t1,sm1calc,'-or');
hold off
% Event 2
sm2 = smcellere{2,:}; t2 = 1:length(sm2);SMF_2 = (1:0.3:SMO(2));good2 = sm2(~isnan(sm2)).';
figure(2)
plot(t2,sm2,'--ob');
hold on
for ai = 1:length(SMF_2)
for aii = 1:length(tau)
SM1 = (SMO(2)-SMF_2(ai))*exp(-t2./tau(aii))+SMF_2(ai);
rmse2(ai,aii) = sqrt(sum((SM1-good2).^2)/length(good2));
end
end
[aai,aaj] = find(rmse2 == min(rmse2(:)));
smf2=SMF_2(aai);tau2 = tau(aaj);
sm2calc = (SMO(2)-smf2)*exp(-t2./tau2)+smf2;
plot(t2,sm2calc,'-or');
hold off
Now, the code is long, and this is only for 2 events. I have 34 events to go through, which I would be doing manually nonetheless if I can't figure out an algorithm to plot every event automatically. Furthermore, a working algorithm can help me cross check my answers.
This is what I'm trying to achieve by meshing everything into a single algorithm:
% Each event is represented as data in each row of cell
% Iterate through each event
for i = 1:length(smcellere)
sm = smcellere{i,:};
% The duration of event is denoted as t
t = 1:length(sm);
% SMO is the 1st data point for each row/event, and SMF is the interval
SMF = (1:0.3:SMO(i));
% Iterate through each data of SMF
for ai = 1:length(SMF)
% Iterate through each day in a year (tau)
for aii = 1:length(tau)
% Plot the initial graph of observed data for each event
figure, plot(t,sm,'--ob');
hold on
% This is the exponential model. Run this model for each
% day in tau and each point in SMF
SM = (SMO(i)-SMF(ai))*exp(-t./tau(aii))+SMF(ai);
for gi = 1:length(SM)
for ki = t
% Calculate Root Mean Square Error to plot as line of
% best fit
rmse(ai,aii) = sqrt(sum((SM(gi)-sm(ki)).^2)/length(sm(ki)));
% THis is the best RMSE
[aai,aaj] = find(rmse == min(rmse(:)));
smf=SMF(aai);tau2 = tau(aaj);
% Calculate the model results
smcalc = (SMO(i)-smf)*exp(-t./tau2)+smf;
end
end
end
end
% Plot line of best fit
plot(t,smcalc,'-or');
hold off
end
The problem is, I can't do trial and error with this because my computer will just freeze due to bad coding.
Thanks in advance for any who'd like a challenge.
  댓글 수: 2
Jonathan Cheong
Jonathan Cheong 2021년 3월 22일
Cool, I've tried a few ways and am happy I'm finally one step closer. The code works perfectly, but only for the first 12 events. At event 13 it gives me an error:
"Index exceeds the number of array elements"
And I can't figure out why. I suspect the problem is marked with (*) , but do not know how to solve it.
smtmp = smcellere(:);
for i = 1:length(smtmp)
sm = smtmp{i,:}.';
t = 1:length(sm);
SMF = (1:0.1:SMO(i));
good = sm(~isnan(sm)).';
figure, plot(t,sm,'--ob');
hold on
for ai = 1:length(SMF)
for aii = 1:length(tau)
SM = (SMO(i)-SMF(ai))*exp(-t./tau(aii))+SMF(ai);
% **rmse(ai,aii) = sqrt(sum((SM-sm).^2)/length(sm));**
end
end
[aai,aaj] = find(rmse == min(rmse(:)));
% if aai > length(SMF)
% smf=SMF(length(SMF));tau2 = tau(aaj);
% elseif aai < length(SMF)
smf=SMF(aai);tau2 = tau(aaj);
% end
smcalc = (SMO(i)-smf)*exp(-t./tau2)+smf;
plot(t,smcalc,'-or');
hold off
end
Any input is much appreciated.
darova
darova 2021년 3월 22일
I don't see rmse preallocation. Did you define size of the variable?

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

채택된 답변

Jonathan Cheong
Jonathan Cheong 2021년 3월 23일
Thanks this works perfectly.
smtmp = smcellere(:);
for i = 1:length(smtmp)
sm = smtmp{i,:}.';
t = 1:length(sm);
SMF = (1:0.1:SMO(i));
figure, plot(t,sm,'--ob');
hold on
rmse = zeros(size(SMF,1),length(tau));
for ai = 1:length(SMF)
for aii = 1:length(tau)
SM = (SMO(i)-SMF(ai))*exp(-t./tau(aii))+SMF(ai);
rmse(ai,aii) = sqrt(sum((SM-sm).^2)/length(sm));
end
end
[aai,aaj] = find(rmse == min(rmse(:)));
smf=SMF(aai);tau2 = tau(aaj);
smcalc = (SMO(i)-smf)*exp(-t./tau2)+smf;
plot(t,smcalc,'-or');
hold off
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by