フィルターのクリア

Need help with finding distances in a matrix program

1 回表示 (過去 30 日間)
Sebastian
Sebastian 2014 年 1 月 25 日
コメント済み: Sebastian 2014 年 1 月 25 日
I have an A matrix, in which I need to find the distances between the points. I have the formulas, but I need help to translate this into matlab.
Lets say that a is equal to:
A=
0 1 0 0 1 0
1 0 0 0 0 0
0 0 0 0 1 0
I have a program to create a new matrix called B,finding this points in which the first column is the number of point, the second column is the x coordinate of the point and the third column is the y coordinate. In this case will be:
B=
1 2 1
2 5 1
3 1 2
4 5 3
Here is the code for this program:
clear z
p=1;
[row,col] = size(A);
for i = 1:row
for j=1:col
if(A(i,j)>0)
z(p,:)=[j,i];
p=p+1;
end
end
end
B=[(1:p-1)',z] %B matrix 1 col is #of point, 2 col is xcoord,3 col is ycoord
I need to find the distances between these points, in this case will be dijx(x distance) and dijy (y distance). For this matrix these will be the distances(absolute value from one point to another point):
dijx=
0 3 1 3
3 0 4 0
1 4 0 4
3 0 4 0
dijy=
0 0 1 2
0 0 1 2
1 1 0 1
2 2 1 0
Can you help me to create a for loop code to find those distances? Thank you!!

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 1 月 25 日
編集済み: Andrei Bobrov 2014 年 1 月 25 日
[r,c]=find(A');
dij = dist([r,c]'); % dist from Neural Network Toolbox
or
[r,c]=find(A');
dijx = bsxfun(@minus,r,r');
dijy = bsxfun(@minus,c,c');
dij = hypot(dijx,dijy);
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2014 年 1 月 25 日
l = A > 8;
[r,c]=find(l');
dij = dist([r,c]');
Sebastian
Sebastian 2014 年 1 月 25 日
Thank you!!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 1 月 25 日
You can get B in two lines of code:
[rows, columns] = find(A)
B = [[1:length(rows)]', rows, columns]
Now I'm not sure what distances you are after are you wanting every distance from N points to the other N-1 points? So you'll have every possible pairing and N*(N-1) distance?
  3 件のコメント
Sebastian
Sebastian 2014 年 1 月 25 日
example file
Image Analyst
Image Analyst 2014 年 1 月 25 日
Well, you can flip the order with
[rows, columns] = find(A')

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by