MATLAB Answers

how do I go about creating and storing data for a monte carlo simulation?

9 views (last 30 days)
Fede C 2018 London
Fede C 2018 London on 22 Oct 2019
Edited: Fede C 2018 London on 22 Oct 2019
Hi all,
This is my ham-fisted attempt to assign memory for the above. Let's say that I want my simulated data to be normally distributed random numbers (they're not, but right now I just care about having a structure in place) arranged in 3 series of 226 observations. So I create a cell that contains the 226x3 (empty, for now) arrays. Let's say I want 4 simulations, so 4 four of those (that's rubbish,but, again, just to keep things simple).
Then I create a 3 dimensional array of the right dimensions with said normal, random data. At this point I ought to extract each 226x3 lots of normal random data and shove it in the cell. Presumably, at that point, I can just put my model in that second loop and do what I need to do.
I can't seem to be able to do the extracting bit. M only contains the last 226x3 block from the 3 dimensional array (rather than all 4).
clc;
clear;
par.muorig = 5; %mean
par.sigmaorig = 5; %stdev
Nsim = 4; % number of simulations
Lseries= 226;
M=cell(4,1);
%allocate memory for matrices-which perhaps I can store in cells
for imatrices=1:Nsim
M{imatrices}=zeros(Lseries,3);
end
for isim= 1:Nsim
data(:,:,isim)=normrnd(par.muorig,par.sigmaorig,Lseries,3);
ff=1
for rr=1:Nsim
M=data(:,:,isim); %pull out each array and shove it in the cells
ff=ff+1
%my model here
end
end
Any suggestions on how to do this? I look forward to hearing from you!

Answers (1)

Fede C 2018 London
Fede C 2018 London on 22 Oct 2019
Edited: Fede C 2018 London on 22 Oct 2019
I think this does it. For those who might need to do something similar:
clc;
clear;
par.muorig = 5;
par.sigmaorig = 5;
Nsim = 4; % number of simulations
Lseries= 226; %sample size
M=cell(4,1);
for imatrices=1:Nsim
M{imatrices}=zeros(Lseries,3);
for isim= 1: Nsim
M{isim}=normrnd(par.muorig,par.sigmaorig,Lseries,3);
end
%my model here
end

Community Treasure Hunt

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

Start Hunting!

Translated by