Save number of iterations in a for loop

7 ビュー (過去 30 日間)
Rajvi Amle
Rajvi Amle 2021 年 7 月 13 日
編集済み: Scott MacKenzie 2021 年 7 月 14 日
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 件のコメント
Rajvi Amle
Rajvi Amle 2021 年 7 月 14 日
@Scott MacKenzie Its okay Sir. Anyways thank you so much for your help and time.
Scott MacKenzie
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
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
Scott MacKenzie 2021 年 7 月 14 日
編集済み: Scott MacKenzie 2021 年 7 月 14 日
Yes, that's true. But, so too does the original script.
Sulaymon Eshkabilov
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
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
  1 件のコメント
Rajvi Amle
Rajvi Amle 2021 年 7 月 14 日
編集済み: Rajvi Amle 2021 年 7 月 14 日
@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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by