How can I find distances to the chosen point from all the other points ( Euclidean distance ) ?

1 回表示 (過去 30 日間)
Hi all, Let's assume that I have a huge matrix and say that I pick a point from that matrix. Is there a way of calculating euclidean distance from all the other points of that matrix and store them separately in the matrix of the same dimension where the number in each entry of a matrix is a number that represents the Euclidean distance to the pre-chosen point ?? Thank you

採用された回答

Image Analyst
Image Analyst 2017 年 6 月 27 日
Assuming you mean in spatial space (which ignores the actual matrix values and just looks at their location), and not in intensity or value space (which compares differences in intensity or value of the matrix to some other intensity or value), then you can do:
% Setup
rows = 4; % For example
columns = 5;
% x and y can be column and row indexes, respectively, but they don't have to be.
x = 1; % For example
y = 1;
% Now compute the distances from every element to (x, y).
[XGrid, YGrid] = meshgrid(1:columns, 1:rows)
distances = sqrt((x - XGrid) .^ 2 + (y - YGrid) .^ 2)
  7 件のコメント
Image Analyst
Image Analyst 2017 年 6 月 28 日
It looks like that's what Jan's code does. You can have his X be any number of rows and columns. And Point would be X(someRowIndex, someColumnIndex).
Damian Wierzbicki
Damian Wierzbicki 2017 年 6 月 28 日
B = zeros (rows , columns );
for c = 1:columns
for r = 1:rows
if ref_matrix ( r , c ) == 0
B = 0;
elseif ref_matrix ( r , c ) ~= 0
B = ( r , c ) = distances ( r , c );
end
end
end
% found a mistake, this a working version.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2017 年 6 月 26 日
編集済み: Jan 2017 年 6 月 26 日
Hm, yes. Why not?
X = rand(1000, 3);
Pick = 17;
Point = X(Pick, :);
Dist = sqrt(sum((X - Point) .^ 2, 2)); % >= R2016b
% Dist = sqrt(sum(bsxfun(@minus, X, Point) .^ 2, 2)); % < R2016b
If this is time critical: FEX: DNorm2

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by