用于PARFOR循环的MATLAB切片变量 [英] MATLAB Slicing variable for PARFOR loops
问题描述
我正在尝试在MATLAB
中使以下循环并行友好,以便可以使用parfor
:
I am trying to make the following loop parallel-friendly in MATLAB
so that I can use parfor
:
for ivert = 1 : nVerts
b = obj.f( obj.neighIDs{ ivert } );
x = obj.coeffMatrix{ ivert } \ b;
obj.solution( ivert, : ) = x( 1 : 3 );
end
我尝试根据此处发布的MATLAB文档对变量进行切片:
I tried to slice the variables according to MATLAB documentation posted here:
parfor ivert = 1 : nVerts
i = obj.neighIDs{ ivert };
b = obj.f( i );
A = obj.coeffMatrix{ ivert }
x = A \ b;
obj.solution( ivert, : ) = x( 1 : 3 );
end
但是MATLAB抱怨:
But MATLAB complains that:
Valid indices for `obj` are restricted in PARFOR loops.
有人可以给我一些提示如何在上面的循环中切片变量吗?
Could someone give me some hints how to slice the variables in the above loop?
推荐答案
此处的问题是MATLAB看到了parfor
循环的前三行,并将其视为obj
上的索引表达式-结论是obj
必须是parfor
广播"变量. parfor
循环的最后一行被视为对obj
的索引分配(即使它看起来像是对obj
字段的索引分配).由于obj
已被分类为广播",因此您无法对其进行分配.要解决此问题,我建议您执行以下操作:
The problem here is that MATLAB sees the first three lines of your parfor
loop, and treats those as indexing expressions on obj
- and it concludes that obj
must be a parfor
"broadcast" variable. The final line of your parfor
loop is treated as an indexed assignment into obj
(even though it looks like an indexed assignment into a field of obj
). Because obj
has been classified as "broadcast", you cannot assign into it. To fix this, I'd recommend doing something like this:
tmpSolution = zeros(nVerts, 3);
parfor ivert = 1:nVerts
... %# calculate 'x'
tmpSolution(ivert, :) = x(1:3);
end
obj.solution = tmpSolution;
这篇关于用于PARFOR循环的MATLAB切片变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!