Find the distance from one point in a matrix to all other points in a matrix

后端 未结 3 1615
南笙
南笙 2020-12-03 20:18

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

3条回答
  •  我在风中等你
    2020-12-03 20:59

    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
    

提交回复
热议问题