Save number of iterations in a for loop
4 次查看(过去 30 天)
显示 更早的评论
I have a for loop, but every iteration overwrites the variable and I have only the final data left.. how can I save data from every loop? Following is my code:
n=10;
t=[];
x=[];
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT);
t=[t, {t0}];
x=[x, {x0}];
end
saves only the last value when i=10. how do I get the other values for (t) and (x)?
Any help would be really appreciated. Thank You in advance.
13 个评论
Scott MacKenzie
2021-7-14
@Rajvi Amle Now that we've established that the t and x data are indeed collected in your script, I've posted an answer demonstrating one way to extract the data from the variables and plot the results for the 10 simulations. There might be different or more appropriate ways to aggregate the data or compute summary statistics, but I'll leave this for you to explore. Good luck.
采纳的回答
Sulaymon Eshkabilov
2021-7-14
An easy solution is:
n=10;
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT);
time{i}=t0;
X{i}=x0;
end
4 个评论
Scott MacKenzie
2021-7-14
编辑:Scott MacKenzie
2021-7-14
Yes, that's true. But, so too does the original script.
Sulaymon Eshkabilov
2021-7-14
Just to see all iteration values the collected cell array can be converted into a matrix array after the loop, e.g:
...
XX = cell2mat(X);
Time=cell2mat(time);
更多回答(1 个)
Scott MacKenzie
2021-7-14
编辑:Scott MacKenzie
2021-7-14
@Rajvi AmleThis script shows one approach to plotting the results of your simulations. The data loaded are the t and x data collected in the script in your question. I've organized the data from each simulation into 3 (6x1) dimensions as you mentioned in a comment. You can explore other aggregations, as appropriate.
% loads the data for 10 simulations into t and x
load('testdata.mat');
% plot simulation results
tiledlayout(2,5);
for i=1:length(t)
% convert data for each iteration to double matrices
tm = cell2mat(t(i)); % 1 column (time steps)
xm = cell2mat(x(i)); % 18 columns (dynamic states)
% aggregate into 3 columns (6 columns for each dimension)
x3 = [mean(xm(:,1:6),2), mean(xm(:,7:12),2), mean(xm(:,13:18),2)];
nexttile;
plot(tm,x3);
title(sprintf('Simulation #%d', i));
legend('Dim 1', 'Dim 2', 'Dim 3');
end

另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!