Efficient memory allocation for MonteCarlo simulation
이전 댓글 표시
Hi, I am a beginner with Matlab and have an issue, maybe someone can help I need to perdorm a MC simulation, I have data for 8760 hours (1 year), i need to create a normal distribution for a variable Vf, and run the code at least 10000 times,
The code is like
n=10000;
L=length(data_es1);
for i=1:1:n
%L is 8760, data_es1(j,2) is the mean while data_es1(j,12) is st.deviation
for j=1:1:L
Vf(j,i)=data_es1(j,2)+data_es1(j,12).*randn;
end
This would create a 10000x8760 matrix, that is very big, and I have several variables to manage in this way (Vf plus at least 5 others), I have encourred a "out of memory" problem,
Any idea to run the code smarter and save memory/busy problems?
Thank you
Alex
댓글 수: 6
KSSV
2017년 10월 20일
You can skip the second loop and write as..
Vf(:,i)=data_es1(:,2)+data_es1(:,12).*randn(8760,1);
In fact you need not to use loops at all..you can do matrix multiplication completely. Where/How you have the data data_es1?
Jan
2017년 10월 20일
Is Vf pre-allocated before the loop?
Alessandro Cerrano
2017년 10월 20일
dpb
2017년 10월 20일
While the vectorized solution will run faster, it won't help on the memory problem -- in fact the second suggestion would create yet another 10K*8760 array; it would need every random variable at one time for every simulation.
The memory savings would have to come from being able to compute what it is you need from the vf variable for each iteration instead of saving every iteration to the end of the loop over the number of simulations.
Alessandro Cerrano
2017년 10월 20일
Jan
2017년 10월 20일
Again: Is Vf pre-allocated before the loop?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!