发现从一个点以矩阵的所有其他点的距离的矩阵

发现从一个点以矩阵的所有其他点的距离的矩阵

本文介绍了发现从一个点以矩阵的所有其他点的距离的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵 A ,我想计算的从一个点到所有其他点的距离即可。所以,真正的结局矩阵应该有一个零(点我选择),并应表现为某种围绕特定点数的圈子。

这是我已经但是我似乎无法得到正确的结果。

  A = [1 2 3 4 5 6 7 8 9 10]对于i = 2:20
    一个(ⅰ,:) = A(I-1,:) + 1;
结束N = 10为I = 1:N
    对于J = 1:N
        DX = A(I,1)-a(J,1);
        DY = A(I,2)-a(J,2);
        距离(I,J)= SQRT(DX ^ 2 + DY ^ 2)
    结束
结束


解决方案

这就是我一直在寻找,但感谢所有的建议。

  A =兰特(5,5);
select_cell = [3 3];
距离=零(尺寸(A,1),尺寸(A,2));
对于i = 1:尺寸(A,1)
    对于j = 1:尺寸(A,2)
        距离(I,J)= SQRT((ⅰ - select_cell(1))^ 2 +(J - select_cell(2))^ 2);
    结束
结束
DISP(距离)

您也可以通过矢量化改进:

 距离=开方((X-X中心值)^ 2 +(Y-yCenter)^ 2

I have a matrix a and I want to calculate the distance from one point to all other points. So really the outcome matrix should have a zero (at the point I have chosen) and should appear as some sort of circle of numbers around that specific point.

This is what I have already but I cant seem to get the correct outcome.

a = [1 2 3 4 5 6 7 8 9 10]

for i = 2:20
    a(i,:) = a(i-1,:) + 1;
end

N = 10

for I = 1:N
    for J = 1:N
        dx = a(I,1)-a(J,1);
        dy = a(I,2)-a(J,2);
        distance(I,J) = sqrt(dx^2 + dy^2)
    end
end
解决方案

This is what i was looking for, but thanks for all the suggestions.

A = rand(5, 5);
select_cell = [3 3];
distance = zeros(size(A, 1), size(A, 2));
for i = 1:size(A, 1)
    for j = 1:size(A, 2)
        distance(i, j) = sqrt((i - select_cell(1))^2 + (j - select_cell(2))^2);
    end
end
disp(distance)

Also you can improve it by using vectorisation:

distances = sqrt((x-xCenter).^2+(y-yCenter).^2

这篇关于发现从一个点以矩阵的所有其他点的距离的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 09:56