我现在试图在Matlab中进行并行计算,并希望使用parfor循环来提高效率。问题是我可以保证每个循环都是相互独立的,但我最终需要更新一个全局变量(也许在Matlab中称为广播变量),当我想给它赋值时有一个问题说它不能被分类。如果我仍然想在这个Matlab中做到这一点,我该如何解决这个问题,或者有什么其他方式可以尝试提高效率?使用parfor的广播变量
的代码是这样的:
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
+0
最后一行有一个错字,是这个问题吗? '原子(iBegin,iEnd,:)'应该是'Atoms(iBegin:iEnd,:)'(第一个''应该是':') –
+0
哪一个是你的全局变量?没有我们可以运行的代码示例,很难理解你的问题。 –
+0
PetrH你是对的,我其实并不是很熟悉Matlab ......但是我认为它在我改变它之后仍然不起作用:( –