parfor中的slice有特定的使用约束
>> a=zeros(100,1);
parfor ii = 1:50
a(2*ii)=1;
end
以上代码运行出错,信息如下:
Error: Unable to classify the variable 'a' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".
根据帮助文件
Ensure That parfor
-Loop Variables Are Consecutive Increasing Integers
需要确保循环变量是连续增加的整数。有三个条件:
(1)整数;
(2)连续;
(3)增加;
以下三种均为错误情况:
parfor i = 0:0.2:1 % not integers不是整数
parfor j = 1:2:11 % not consecutive不是连续增加
parfor k = 12:-1:1 % not increasing不增加
对于非整数情况,可以通过如下方法更改
iValues = 0:0.2:1; parfor idx = 1:numel(iValues) i = iValues(idx); ... end