How can I write results from parfor loop into multidimensional array?
显示 更早的评论
I'm using a parfor loop (2000 loops) and in every run of the parfor loop I loop over a predefined vector of parameter values (length=10) and within this loop I simulate over 800 periods for each parameter value. I'm using a function within the parfor loop, because I have to pass information from a cell array into the parfor loop. I would like to save all the results in a three-dimensional matrix, however this does not work and I don't understand why.
What I would like to do:
utilmatrix_diff_monpol = NaN(2000,800,10);
parfor i=1:2000
[aa,bb,cc,dd,ee,ff,gg,hh,ii,jj]=wrapper_function_monpol(db,dbaux,bettavect,nosimul,exog_varlist,nexog_varlist);
utilmatrix_diff_monpol(i,:,1)=aa;
utilmatrix_diff_monpol(i,:,2)=bb;
utilmatrix_diff_monpol(i,:,3)=cc;
utilmatrix_diff_monpol(i,:,4)=dd;
utilmatrix_diff_monpol(i,:,5)=ee;
utilmatrix_diff_monpol(i,:,6)=ff;
utilmatrix_diff_monpol(i,:,7)=gg;
utilmatrix_diff_monpol(i,:,8)=hh;
utilmatrix_diff_monpol(i,:,9)=ii;
utilmatrix_diff_monpol(i,:,10)=jj;
end
But his gives me an error message:
Error: Unable to classify the variable 'utilmatrix_diff_monpol' in the body of
the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve
Variable Classification Issues in parfor-Loops".
However, parfor in general does not have a problem with writing something into a multidimensional array, only if I call it more than once. The following works:
utilmatrix_diff_monpol = NaN(2000,800,10);
parfor i=1:2000
[aa,bb,cc,dd,ee,ff,gg,hh,ii,jj]=wrapper_function_monpol(db,dbaux,bettavect,nosimul,exog_varlist,nexog_varlist);
utilmatrix_diff_monpol(i,:,1)=aa;
end
But that does not help me. So, what I do now, is to create a new vector for every parameter value I use inside the loop (wrapper_function_monpol)
parfor i=1:2000
[aa,bb,cc,dd,ee,ff,gg,hh,ii,jj]=wrapper_function_monpol(db,dbaux,bettavect,nosimul,exog_varlist,nexog_varlist);
util_monpol2(i)=bb;
util_monpol3(i)=cc;
util_monpol4(i)=dd;
util_monpol5(i)=ee;
util_monpol6(i)=ff;
util_monpol7(i)=gg;
util_monpol8(i)=hh;
util_monpol9(i)=ii;
util_monpol10(i)=jj;
end
But this gets quite messy when I have more than 10 cases. Also, I just don't get why the parfor loop won't accept writing multiple times into one multidimensional array. It seems to see a problem where there actually isn't one.
回答(1 个)
Walter Roberson
2022-7-18
utilmatrix_diff_monpol(i,:,1)=aa;
utilmatrix_diff_monpol(i,:,2)=bb;
When you use parfor, the output variables indexed by the parfor index variable, must all use the same indices. For example it would be valid to have multiple assignments to utilmatrix_diff_monpol(i,:,1) but not to have an assignment to utilmatrix_diff_monpol(i,:,1) and another to utilmatrix_diff_monpol(i,:,2)
The solution is to pack all of the aa, bb, and so on, into a single 2D array, and then assign that 2d array to utilmatrix_diff_monpol(i,:,:)
类别
在 帮助中心 和 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!