我正在训练我自己的自组织地图来聚类颜色值。现在我想制作某种U 矩阵来显示节点与其直接邻居之间的欧几里德距离。我现在的问题是,我的算法效率很低!!肯定有一种方法可以更有效地计算它吗?
function displayUmatrix(dims,weights) %#dims is [30 30], size(weights) = [900 3],
%#consisting of values between 1 and 0
hold on;
axis off;
A = zeros(dims(1), dims(2), 3);
B = reshape(weights',[dims(1) dims(2) size(weights,1)]);
if size(weights,1)==3
for i=1:dims(1)
for j=1:dims(2)
if i~=1
if j~=1
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i-1,j-1,:)).^2;
end
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i-1,j,:)).^2;
if j~=dims(2)
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i-1,j+1,:)).^2;
end
end
if i~=dims(1)
if j~=1
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i+1,j-1,:)).^2;
end
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i+1,j,:)).^2;
if j~=dims(2)
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i+1,j+1,:)).^2;
end
end
if j~=1
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i,j-1,:)).^2;
end
if j~=dims(2)
A(i,j,:)=A(i,j,:)+(B(i,j,:)-B(i,j+1,:)).^2;
end
C(i,j)=sum(A(i,j,:));
end
end
D = flipud(C);
maximum = max(max(D));
D = D./maximum;
imagesc(D)
else
error('display function does only work on 3D input');
end
hold off;
drawnow;
结尾
谢谢,马克斯