Clear Filters
Clear Filters

How to save all the simulated iterations (output of loop) togather?

3 views (last 30 days)
This is my code and each and five out parameters are vp0, vs0, rh0, qp0, qs0 each of size (115*50). I want to save out put vp0, vs0, rh0, qp0, qs0 as of size (115 50) but instead each time I have to change k as from 1 then 2, 3 and so on...
Simply means, each time I have to save individual vp0(1), vp0(2), vp0(3) and so on... How I can save vp0(115*5) all togather?
for k=1:50
print k
msim = mvnrnd(Mu_m, Sigma_m)';
msimlogs = reshape(msim,nt,5);
h1=subplot(1,5,1); hold on; plot(msimlogs(:,1),t); hold on; plot(vp0,t,'k','LineWidth',1.5); title('Simulated V_P'); ylim([1852 1965]);
h2=subplot(1,5,2); hold on; plot(msimlogs(:,2),t); hold on; plot(vs0,t,'k','LineWidth',1.5); title('Simulated V_S'); ylim([1852 1965]);
h3=subplot(1,5,3); hold on; plot(msimlogs(:,3),t); hold on; plot(rho0,t,'k','LineWidth',1.5); title('Simulated Rho'); ylim([1852 1965]);
h4=subplot(1,5,4); hold on; plot(msimlogs(:,4),t); hold on; plot(qp0,t,'k','LineWidth',1.5); title('Simulated Q_P'); ylim([1852 1965]);
h5=subplot(1,5,5); hold on; plot(msimlogs(:,5),t); hold on; plot(qs0,t,'k','LineWidth',1.5); title('Simulated Q_S'); ylim([1852 1965]);
msimsave(:,:,k) = msimlogs;
set(h1,'YDir','Rev'); set(h2,'YDir','Rev'); set(h3,'YDir','Rev'); set(h4,'YDir','Rev'); set(h5,'YDir','Rev');
vp0 = msimsave(:,1,3); vs0 = msimsave(:,2,3); rho0 = msimsave(:,3,3); (can I get here all k, instead 1 array)
qp0 = msimsave(:,4,3); qs0 = msimsave(:,5,3);
save Initial_model.mat vp0 vs0 rho0 qp0 qs0;
k --- is plotted below

Accepted Answer

William Rose
William Rose on 9 Sep 2022
Make a 3D array.
for i=1:5
vp0=rand(115,50); %compute vp0
vp0all(:,:,i)=vp0; %save the currnt vp0 in 3D array
Try it.
Nisar Ahmed
Nisar Ahmed on 10 Sep 2022
Thanks, now it is working, Yes this mean that each of vp0, vs0, rho0... is a matrix with dimensions 115 by 50.
Thank you again

Sign in to comment.

More Answers (0)


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!