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

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

 Accepted Answer

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 Comments

@William Rose not getting the desired answer.
First, in the figure e.g., simulated VP is in m/sec, and by doing as you suggested, values changes like 0.2345 etc.
Second I have five paramaters vp0, vs0, rh0, qp0, qs0 and each has 115 values and 50 arrays.
My code, including vp0=rand(), was intended to illustrate a method of storing data in a 3D array. I did not intend to replace your way of calculating the random signals.
I now believe that you intended to type 50, not 5, when you typed "How I can save vp0(115*5) all together?"
You wrote, in your comment, "I have five paramaters vp0, vs0, rh0, qp0, qs0 and each has 115 values and 50 arrays." Does this mean that each is a matrix with dimensions 115 by 50? They do not have that size inside the for loop. They are each vectors inside the for loop.
Your code includes the loop for k=1:50, ...; end. vp0, vs0, etc. are vectors, and they are not calculated inside the loop. They must have been calculated before the loop. After the for loop completes, you attempt to re-define vp0, vs0, etc., as arrays. It is confusing for a single variable name to represent different size objects at different parts of the code. I recommend that you use distinct names for the vector and matrix versions of vs0, vp0, etc. For example, use vp00, vs00, etc. for the vectors, which do not change during the loop. Use vp0, vs0, etc., for the matrices, which will be defined after the loop completes.
msimlogs is nt-by-5 array. It changes value on each loop pass. msimsave is nt-by-5-by-50. msimlogs is saved as a different page of 3D array msimsave, on each loop pass. Therefore, when the loop is complete, the 3D array msimsave contains the data from all 50 passes. I assume from your comments that nt=115, although this is not clear from the code.
I think you want to extract all 50 vp0 traces, and all 50 vs0 traces, etc., from msimsave, after the loop completes.
First, let us create the 3D array msimsave, to illustrate the concept. msim save will actually be filled up during the 50 loop passes.
msimsave=randn(115,5,50);
Then do the following, after the for k=1:50 loop completes:
vp0=msimsave(:,1,:); %create 3D array with dimensions 115x1x50
vp0=squeeze(vp0); %now vp0 is 2D array with dimensions 115x50
vs0=msimsave(:,2,:);
vs0=squeeze(vs0);
rh0=msimsave(:,3,:);
rh0=squeeze(rh0);
qp0=msimsave(:,4,:);
qp0=squeeze(qp0);
qs0=msimsave(:,5,:);
qs0=squeeze(qs0);
The code above creates 5 arrays, each with size 115x50.
Illustrate that vp0 is 115x50:
disp(size(vp0))
115 50
Good luck.
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
@Nisar Ahmed, you're welcome. Good luck with your work.

Sign in to comment.

More Answers (0)

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!