【发布时间】:2015-05-06 21:13:29
【问题描述】:
我现在正在尝试在 Matlab 中进行并行计算,并希望使用 parfor 循环来提高效率。问题是我可以保证每个循环彼此独立,但我最终需要更新一个全局变量(在 Matlab 中可能称为广播变量),当我想为其分配一些值时,有一个问题说它不能被分类。如果我仍然想在这个 Matlab 中做,我该如何解决这个问题或者有没有其他方法可以尝试提高效率?
代码是这样的:
Atoms(1:nOfAtomsInTwoDim,:)=TwoDimAtoms;
odd_type=TwoDimAtoms;
even_type=TwoDimAtoms;
even_type(:,1)=TwoDimAtoms(:,1)+LatticeSpacing/2;
even_type(:,2)=TwoDimAtoms(:,2)+LatticeSpacing/2;
parfor i=2:1:nOflayers+1
temp_type=TwoDimAtoms;
if mod(i,2)
temp_type=odd_type;
temp_type(:,3)=TwoDimAtoms(:,3)+(i-1)*LatticeSpacing/2;
else
temp_type=even_type;
temp_type(:,3)=TwoDimAtoms(:,3)+(i-1)*LatticeSpacing/2;
end
iBegin=(i-1)*nOfAtomsInTwoDim+1;
iEnd=i*nOfAtomsInTwoDim;
Atoms(iBegin,iEnd,:)=temp_type;
end
【问题讨论】:
-
最后一行有错字,是这个问题吗?
Atoms(iBegin,iEnd,:)应该是Atoms(iBegin:iEnd,:)(第一个,应该是:) -
你的全局变量是哪一个?如果没有我们可以运行的代码示例,很难理解您的问题是什么。
-
PetrH 你说得对,其实我对Matlab不是很熟悉……但是我觉得改了之后还是不行:(
-
大卫,全局是 Atoms,它是一个 n×3 矩阵。