Performance MATLAB代码在MacBookPro上运行缓慢,三重while循环
我已经运行了将近六个小时的MATLAB程序,但它仍然没有完成。它在三个while循环中循环(外部两个循环为n=855,内部循环为n=500)。花了这么长时间,这是一个惊喜吗?我能做些什么来提高速度吗?我包括下面的代码,以及下面的变量数据类型Performance MATLAB代码在MacBookPro上运行缓慢,三重while循环,performance,algorithm,matlab,runtime,execution,Performance,Algorithm,Matlab,Runtime,Execution,我已经运行了将近六个小时的MATLAB程序,但它仍然没有完成。它在三个while循环中循环(外部两个循环为n=855,内部循环为n=500)。花了这么长时间,这是一个惊喜吗?我能做些什么来提高速度吗?我包括下面的代码,以及下面的变量数据类型 while i < (numAtoms + 1) pointAccessible = ones(numPoints,1); j = 1; while j <(numAtoms + 1)
while i < (numAtoms + 1)
pointAccessible = ones(numPoints,1);
j = 1;
while j <(numAtoms + 1)
if (i ~= j)
k=1;
while k < (numPoints + 1)
if (pointAccessible(k) == 1)
sphereCoord = [cell2mat(atomX(i)) + p + sphereX(k), cell2mat(atomY(i)) + p + sphereY(k), cell2mat(atomZ(i)) + p + sphereZ(k)];
neighborCoord = [cell2mat(atomX(j)), cell2mat(atomY(j)), cell2mat(atomZ(j))];
coords(1,:) = [sphereCoord];
coords(2,:) = [neighborCoord];
if (pdist(coords) < (atomRadius(j) + p))
pointAccessible(k)=0;
end
end
k = k + 1;
end
end
j = j+1;
end
remainingPoints(i) = sum(pointAccessible);
i = i +1;
end
而i<(numtoms+1)
pointAccessible=1(numPoints,1);
j=1;
当j尝试矢量化它时,看看这里如何处理它:谢谢,这对我来说是新的。我会试着去想这个问题。我的代码有点复杂,因为它包括三个while循环;我试图弄清楚我是否只能“矢量化”内部循环(并将SphereCord和neighborCoord更改为vectors)…首先,您可以用atomX{I}替换cell2mat(atomX(I))
-避免为每个元素调用cell2mat的开销-因为这是访问cell-content的正确方式。请从您的问题中删除对MacBook的引用,它们完全无关。计算耗时如此之长表明您需要升级算法,而不是升级计算机。
numAtoms = 855
numPoints = 500
p = 1.4
atomRadius = <855 * 1 double>
pointAccessible = <500 * 1 double>
atomX, atomY, atomZ = <1 * 855 cell>
sphereX, sphereY, sphereZ = <500 * 1 double>
remainingPoints = <855 * 1 double>