我在 matlab 程序中有一个嵌套三次的 for 循环。谁能帮我优化一下。
w=5;
a = rand(m*n,10); b=rand(m,n);
for i = 1 : m
for j = 1 : n
for k = 1 : l
if (i-w >= 1 && i+w <= m)
featureL = a(((i-1)*n)+j,:); featureR = a(((i-1)*n)+j-d,:);
D1(i,j,k) = sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) );
D2(i,j,k) = mean2( b(i-w:i+w, j-w:j+w) );
end
end
end
end
我知道使用 meshgrid 可以大大提高性能,但我不知道该怎么做。
感谢期待。
可以做这样的事情吗..
[X Y Z] = meshgrid(1:m,1:n,1:l);
D1(something containing X,Y,Z) = sqrt( sum( ( a(something cont. X,Y) - a(something cont. X,Y)).*(a(something cont. X,Y) - a(something cont. X,Y)) ) );
% similarly D2
非常感谢!。