用于PARFOR循环的MATLAB切片变量 [英] MATLAB Slicing variable for PARFOR loops

查看:908
本文介绍了用于PARFOR循环的MATLAB切片变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆