在MATLAB中使用parfor.切片变量和嵌套循环 [英] parfor in matlab. sliced variable and nested loop

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

问题描述

我尽力遵循并行工具箱的文档,但是仍然无法避免重复使用嵌套循环中索引的数组的问题. 问题出在变量 node

I did my best to follow the documentation of the parallel toolbox, but still I could not avoid the problem of reusing array that was indexed in a nested loop. The problem is with variable node

parfor i=1:nX
   for j=1:nY


    [ind,dist]=findInCircle(node(i,j,:), part,r);

    UV=calcVelocity(part(ind,:), dist,node(i,j,:)) ;


    %here matlab complains that node is not indexed properly
    node(i,j,3)= UV(1);
    node(i,j,4)= UV(2);
    node(i,j,5)= UV(3);



   end


end

我不在嵌套循环之外使用数组,索引也是根据规则进行的.我是否错过了另一个 parfor 限制?

I do not use the array outside of the nested loop, the indexing is also according to the rule. Did I miss another parfor restriction?

推荐答案

根据文档,您不能像以前那样使用其他索引:

According to the documentation you can not use different indices like you did:

在第一级括号或大括号内,对于所有出现的给定变量,索引列表都是相同的.

一个简单的解决方法是可能的:

A simple workaround is possible:

parfor i=1:nX
   nodeSlice=node(i,:,:)

   for j=1:nY


    [ind,dist]=findInCircle(nodeSlice(j,:), part,r);

    UV=calcVelocity(part(ind,:), dist,nodeSlice(j,:)) ;


    %here matlab complains that node is not indexed properly
    nodeSlice(j,3)= UV(1);
    nodeSlice(j,4)= UV(2);
    nodeSlice(j,5)= UV(3);



   end
   node(i,:,:)=nodeSlice;

end

从包含所有索引的矩阵中获取一个切片,对其进行处理,然后将其返回.

Get a slice from the matrix which contains all indices, work with it and then return it.

这篇关于在MATLAB中使用parfor.切片变量和嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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