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

조회 수: 1 (최근 30일)
Nisar Ahmed
Nisar Ahmed 2022년 9월 9일
댓글: William Rose 2022년 9월 10일
Hi,
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?
figure(3);
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;
end
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

채택된 답변

William Rose
William Rose 2022년 9월 9일
Make a 3D array.
vp0all=zeros(115,50,5);
for i=1:5
vp0=rand(115,50); %compute vp0
vp0all(:,:,i)=vp0; %save the currnt vp0 in 3D array
end
Try it.
  댓글 수: 4
Nisar Ahmed
Nisar Ahmed 2022년 9월 10일
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
William Rose
William Rose 2022년 9월 10일
@Nisar Ahmed, you're welcome. Good luck with your work.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by