我在 Matlab 中有以下代码(当然是简化的,但这说明了问题的根源),以前使用for
过;我想将其更改为使用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
非常大,保存所有数据输出将非常占用内存。
还有另一种方法吗?