matlab - Using struct arrays in parfor -
i having trouble using struct arrays in matlab's parfor loop. following code has 2 problems not understand:
s=struct('a',{},'b',{}); if matlabpool('size')==0 matlabpool open local 2 end j = 1:2 parfor k=1:4 fprintf('[%d,%d]\n',k,j) s(j,k).a = k; s(j,k).b = j; end end matlabpool close
- it fails error
error using parallel_function (line 589) insufficient number of outputs right hand side of equal sign satisfy assignment.
- on output, variable
s
vector, not array (as should be, if code breaks before finishing).
edit problem solved if initialize struct arrays correct size, by:
s=struct('a',cell(2,4),'b',cell(2,4));
however, still happy insights problem (e.g rally bug, suggested oleg komarov)
it working fine me don't know happens. in general need careful parfor loops , there ample documentation on how align everything. 2 different words of advice. first , more importantly, parfor loop on outside loop:
function s = foo s=struct('a',{},'b',{}); parfor j = 1:2 k=1:4 fprintf('[%d,%d]\n',k,j) s(j,k).a = k; s(j,k).b = j; end end
two, matlab gets picky writing main exit variable (i.e. variable contained in parfor loop indexed loop, in case, s
). first want create dummy variable holds innerloop information, , writes once @ end of loops. example:
function s = khal s=struct('a',{},'b',{}); parfor j = 1:2 dummy=struct('a',{},'b',{}); k=1:4 fprintf('[%d,%d]\n',k,j) dummy(k).a = k; dummy(k).b = j; end s(j,:) = dummy; end
you don't have problem here, can complicated in other instances
Comments
Post a Comment