Distance between a points in 2D and 3D matrix in Matlab
Show older comments
I want to find the distance between a point in a 2D matrix and a point in a 3D matrix. My 2D matrix has the information (num, k) and my 3D has the information (X,Y,num). I would like to use the distance equation, but everytime I try I one form or another I get an error because of dimensions. Is there any way to do this?
In 2D I can compute the information using:
for n = 1:3 ...
for i = 1:3
d(n,i) = sqrt((g(i,1)-A(n,1))^2+(g(i,2)-A(n,2))^2);
dd(n,i) = double(d(n,i));
end
end
In 3D I tried to compute the information using
for n = 3...
for i = 1:3
d(n,i) = sqrt((g(i,1)-A(n,1,:))^2+(g(i,2)-A(n,2,:))^2);
dd(n,i) = double(d(n,i));
end
end
I only started using Matlab recently so I am assuming that if I use a : in the third column of the 3D A matrix, this will "disregard" the 3rd column.
I would appreciate it if someone could lead me in the right direction as to syntex or a basic explanation of the math if I am wrong with it as well.
1 Comment
Matt J
on 20 Mar 2013
Wouldn't d have to be 3-dimensional in the 3D case? You'r e indexing it 2-dimensionally d(n,i).
Answers (1)
This will output ann array indexable as d(i,n,num)
f=@(a,b) (a-b).^2;
d=bsxfun(f, g(:,1), A(:,1,:) ) + bsxfun(f, g(:,2), A(:,2,:) );
d=sqrt(d);
dd=double(d);
2 Comments
Victoria Chang
on 20 Mar 2013
You might need to do
d=bsxfun(f, g(1:3,1).', A(1:3,1,:) ) + bsxfun(f, g(1:3,2).', A(1:3,2,:) );
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!