How to use dynamic variable in Parfor
조회 수: 6 (최근 30일)
이전 댓글 표시
I try to use dynamic variables in a parfor Loop. But the function EVAL can not be called within the loop. examples like
sensorNumber= [1361 1364 1367 1368 1373 1381 1382 1401 1565 1567];
for i=1:length(sensorNumber)
eval(['value' num2str(sensorNumber(i)) '= dust' num2str(sensorNumber(i)) ';']);
end
how can i use parfor to process this kind of thing
댓글 수: 1
답변 (1개)
Walter Roberson
2015년 8월 10일
You cannot use parfor to process that kind of thing.
I suggest
parfor i = sensorNumber
value{i} = dust{i};
end
which I would quickly replace with
value(sensorNumber) = dust(sensorNumber);
with no loop.
댓글 수: 3
Steven Lord
2015년 8월 10일
You cannot and should not. PARFOR needs to analyze the contents of the loop body in preparation for parallelizing it, and I believe that analysis can't be completed if there's an EVAL statement in the loop (since the code to be executed could literally be anything, including something that introduces an ordering to the loop iterations.)
I believe using dynamic field names with a struct array will work in a PARFOR.
Walter Roberson
2015년 8월 10일
편집: Walter Roberson
2015년 8월 10일
Cell arrays indexed by number will work for sure.
Dynamic field names would likely lead to an error about the variable not being classifiable, because the parfor analyzer is not able to interpret the field-name construction code well enough to be able to prove that no field is being duplicated (which would introduce an order dependency)
참고 항목
카테고리
Help Center 및 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!