Arrays 利用Matlab计算多重欧氏距离的有效方法

Arrays 利用Matlab计算多重欧氏距离的有效方法,arrays,matlab,euclidean-distance,Arrays,Matlab,Euclidean Distance,我正在训练我自己的自组织映射来聚类颜色值。现在我想做一些a来显示节点和它们的直接邻居之间的欧几里德距离。我现在的问题是,我的算法效率很低!!有没有更有效的方法来计算 function displayUmatrix(dims,weights) %#dims is [30 30], size(weights) = [900 3], %#consisting of values between 1 and 0 hold o

我正在训练我自己的自组织映射来聚类颜色值。现在我想做一些a来显示节点和它们的直接邻居之间的欧几里德距离。我现在的问题是,我的算法效率很低!!有没有更有效的方法来计算

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;
结束

谢谢,Max

您可以通过以下方法计算每个点到其右侧相邻点的(平方)距离:

sum((B(:,1:end-1,:) - B(:,2:end,:)).^2, 3)
类似地,计算每个点到下方点以及两条对角线上点的距离。边界上的点没有所有这些值,所以用零填充它们。然后将距离相加,然后除以一个点所拥有的邻居数,得到与所有邻居的平均距离

这是我的密码:

%calculate distances to neighbors
right = sum((B(:,1:end-1,:)- B(:,2:end,:)).^2, 3);
bottom = sum((B(1:end-1,:,:)- B(2:end,:,:)).^2, 3); zeros();
diag1 = sum((B(1:end-1,1:end-1,:)- B(2:end,2:end,:)).^2, 3);
diag2 = sum((B(2:end,2:end,:)- B(1:end-1,1:end-1,:)).^2, 3);

%pad them with zeros to the correct size
rightPadded = [right zeros(dim(1) , 1)];
leftPadded = [zeros(dim(1) , 1) right];

botomPadded = [bottom; zeros(1,dim(2))];
upPadded = [zeros(1,dim(2));bottom];

bottomRight = zeros(dim(1), dim(2));
bottomRight(1:end-1,1:end-1) = diag1;
upLeft = zeros(dim(1), dim(2));
upLeft(2:end,2:end) = diag1;

bottomLeft = zeros(dim(1), dim(2));
bottomLeft(1:end-1,2:end) = diag2;
upRight = zeros(dim(1), dim(2));
upRight(2:end,1:end-1) = diag2;

%add distances to all neighbors
sumDist = rightPadded + leftPadded + bottomRight + upLeft + bottomLeft + upRight;

%number of neighbors a point has
neighborNum = zeros(dim(1), dim(2)) + 8;
neighborNum([1 end],:) = 5;
neighborNum(:,[1 end]) = 5;
neighborNum([1 end],[1 end]) = 3;

%divide summed distance by number of neighbors
avgDist = sumDist./neighborNum;
它是矢量化的,所以它应该比你的版本快。
如果您想要精确的U矩阵,可以将平均距离与相邻距离交错。

我建议您沿3个维度中的每个维度使用2D卷积核,一次获得每个点与其所有相邻点之间的总距离,然后将其平方。不过,我不确定这是否是您想要的,因为在代码中,在每一步添加计算距离之前,您都会将其平方。尽管如此,您仍然可以对B矩阵进行几次卷积,例如使用内核[1-1;0-1],一次获得所有的B(i,j,:)-B(i-1,j+1,:)。(如果我弄错了,请有人纠正我)。不过我不确定会不会快一点。谢谢你的快速回答。我想说,这将以任何方式提高代码的可读性。我将在Matlab文档中查找它。做得很好,谢谢,这对时间复杂性有很大帮助!