我在Matlab中有下面的代码(当然简化了,但是这显示了问题的根源),它以前使用for
;我想改变它使用parfor
代替:Matlab - 如何转换此代码,以便它可以使用parfor运行?
isForeground = ones(1, size(In, 1));
% this used to be a simple for
parfor i=1:X
data = output_of_some_expensive_function();
% do some other stuff with data here
% the part below is the problem; isForeground basically keeps track of all the places where data was never a zero
isForeground(data == 0) = 0;
end
Matlab的抱怨说,Valid indices for 'isForeground' are restricted in PARFOR loops
。有没有办法解决这个问题?我想只保存所有data
输出,然后运行一个单独的传统for
循环,我将执行isForeground
部分,但问题是X
非常大,并且保存所有数据输出将会占用大量内存。
是否有另一种方法呢?
这似乎是一个很好的解决方案;然而,我在代码中发现了额外的依赖关系,所以最终看起来我唯一的可能性就是保存所有数据并在最后运行另一个for循环,所以我不会使用它。感谢您的回答,虽然 – houbysoft 2013-05-02 04:02:05
没有问题。祝你好运! – 2013-05-02 04:12:56