Save number of iterations in a for loop

7 views (last 30 days)
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 Comments
Rajvi Amle
Rajvi Amle on 14 Jul 2021
@Scott MacKenzie Its okay Sir. Anyways thank you so much for your help and time.
Scott MacKenzie
Scott MacKenzie on 14 Jul 2021
@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.

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 14 Jul 2021
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 Comments
Scott MacKenzie
Scott MacKenzie on 14 Jul 2021
Edited: Scott MacKenzie on 14 Jul 2021
Yes, that's true. But, so too does the original script.
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 14 Jul 2021
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);

Sign in to comment.

More Answers (1)

Scott MacKenzie
Scott MacKenzie on 14 Jul 2021
Edited: Scott MacKenzie on 14 Jul 2021
@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
  1 Comment
Rajvi Amle
Rajvi Amle on 14 Jul 2021
Edited: Rajvi Amle on 14 Jul 2021
@Scott MacKenzie and @Sulaymon Eshkabilov Thank you so much for your responses and time to help me out, even my code is bit complex to understand. @Scott MacKenzie I got plots for 10 iterations. Further I want to calculate the mean of all these 10 iterations all together. To make you understand better I.ll explain you here. Following is the part of my code:
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,SYSTEM);
t=[t, {t0}];
x=[x, {x0}];
end
After my loop, I have a cell array of x=1x10 cell where each of the 10 arrays are matrices of 138x18. If I want to access the data of each of the cells but just want to calculate the sum of (138,1) and (138,2) from each arrays then how do I calculate and how can I access it? I already tried as shown below:
%% Split state vector and auxiliary vector
% for i=1:n
X = x{1,i}
T = t{1,i}
m_Au_A1=X(:,1); %Amount of AuNP in arterial blood
m_Au_V1=X(:,2); %Amount of AuNP in venous blood
How do I call values stored inside a cell?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!