0

我对 Matlab 很陌生。我使用 parfor 循环来完成一项非常耗时的任务。请参阅下面的片段。但是,我从 Matlab 得到了错误信息。任何人都可以帮忙吗?我阅读了有关 parfor 的文档,但不知道该怎么做...

谢谢你。

The parfor loop cannot run due to the way variable "M" is used

The parfor loop cannot run due to the way variable "T" is used


Explanation 
MATLAB runs loops in parfor functions by dividing the loop iterations into groups, and then sending them to MATLAB workers where they run in parallel. For MATLAB to do this in a repeatable, reliable manner, it must be able to classify all the variables used in the loop. The code uses the indicated variable in a way that is incompatible with classification.




 parfor i=1:size(x,1)

    if (ind(index(i)) == Index1)
        para=lsqcurvefit(F, [M(index(i)) T(index(i))], t, SS(ind(index(i)):end,i), [0 0], [MaxiM(index(i)) maxT],options);
    elseif  (ind(index(i)) == Index2) 
        para=lsqcurvefit(F, [M(index(i)) T(index(i))], t2, SS(ind(index(i)):end,i), [0 0], [MaxiM(index(i)) maxT],options);
    end

end
4

1 回答 1

1

您应该重新组织MT在并行循环中使用它们。这应该有效:

M = M(index);
T = T(index);
parfor i=1:size(x,1)
    if (ind(index(i)) == Index1)
        para = lsqcurvefit(F, [M(i) T(i)], t, SS(ind(index(i)):end,i), ...
                           [0 0], [MaxiM(index(i)) maxT], options);
    elseif (ind(index(i)) == Index2) 
        para = lsqcurvefit(F, [M(i) T(i)], t2, SS(ind(index(i)):end,i), ...
                           [0 0], [MaxiM(index(i)) maxT], options);
    end
end

但是,如果您需要返回函数lsqcurvefit- 那么我同意Kleist的评论,即您的代码毫无意义。

更新:

您可以尝试进行类似的重新排列以进一步提高性能:

M = M(index);
T = T(index);
ind = ind(index);
MaxiM = MaxiM(index);
parfor i=1:size(x,1)
    if (ind(i) == Index1)
        para = lsqcurvefit(F, [M(i) T(i)], t, SS(ind(i):end,i), ...
                           [0 0], [MaxiM(i) maxT], options);
    elseif (ind(i) == Index2) 
        para = lsqcurvefit(F, [M(i) T(i)], t2, SS(ind(i):end,i), ...
                           [0 0], [MaxiM(i) maxT], options);
    end
end
于 2013-02-16T17:13:43.150 回答