calculate the (geometrical) distance between points of a matrix

조회 수: 2 (최근 30일)
Felix
Felix 2012년 1월 27일
Lets say I have a large matrix full of zeros, with a few numbers (1,2,3,4,5) located at various positions. Now I want to calculate the distances from point 1 to 2, 1 to 3, 1 to 4 and 1 to 5. Assuming that e.g. the width of the columns in this matrix is 0.4mm and the width of the rows is 0.25mm. Maybe there is already a code/toolbox out there to do such things?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 1월 27일
eg:
data =[0 0 0 0 0 0 0 0 0 0
0 0 0 0 3 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 4 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 5 0 0
0 0 0 0 0 0 0 0 0 0]
[i1,j1,c] = find(data)
ijc = sortrows([i1,j1,c],3)
d = bsxfun(@minus,ijc(2:end,1:2),ijc(1,1:2))
out = hypot(d(:,1)*.25,d(:,2)*.4)
OR
use function dist from Neural Network Toolbox
c = dist(bsxfun(@times,ijc(:,1:2),[.25 .4]).');
out = c(:,1);
  댓글 수: 2
Felix
Felix 2012년 1월 27일
Thanks, this works great for me, even though I don't understand it.
(I assume here the cells are considered to be infinitesimal points connected with lines of defined lengths?)
I have to check out the documentation of this toolbox, one thing I might want to do is calculate the distance of the path from 1 to 2 to 3, 1 to 2 to 3 to 4 and 1 to 2 to 3 to 4 to 5.
Andrei Bobrov
Andrei Bobrov 2012년 1월 27일
out = diag(dist(bsxfun(@times,ij,[.25; .4])),1)
this is 1 to 2 to 3 to 4 to 5.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Point Cloud Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by