求矩阵中一点到矩阵中所有其他点的距离 [英] Find the distance from one point in a matrix to all other points in a matrix

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

问题描述

我有一个矩阵a,我想计算从一个点到所有其他点的距离.所以实际上结果矩阵应该有一个零(在我选择的点上)并且应该显示为围绕该特定点的某种数字圆圈.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆