MATLAB Answers

sona nil
0

HOW Calculate the distance of points form one center in 2-D space and display output in a distance matrix?

Asked by sona nil
on 7 Sep 2019
Latest activity Commented on by sona nil
on 8 Sep 2019
Hello all, I am new to Matlab and I am having quite a bit of difficulty understanding how to calculate the distance between points with equal distances in a 2-D space. I need to develop a function to create an array of 5 points and find the distances between them. The output of the function will be a distance matrix wich size is 5*5.
I know about "pdist2" but I canot develop this for my problem. I need this matrix(look at the picture). The out put matrix should be a 5*5 Symmetric matrix and the elemet of diagonal should be zero( because the distance of every point with each self will be zero)

  8 Comments

Did you see my other comment?
Because one of the assumptions of my question is that the distance is equal. look at this picture.
That is a totally different question. You never said you needed to generate the coordinates as well. Our functions work for any set of points. If you want to randomly generate a point cloud with exactly the shape you describe, you can do this:
x_center=rand;
y_center=rand;
h=randi([2 5]);
X=x_center+[0 0 h 0 -h];
Y=y_center+[0 h 0 -h 0];
Or this:
x_center=rand;
y_center=rand;
h=randi([2 5]);
phi_offset=2*pi*rand;
phi=[0.5 0 -0.5 -1]*pi;
X=x_center+[0 h*cos(phi_offset+phi)];
Y=y_center+[0 h*sin(phi_offset+phi)];
fine, I think this is what I need. Thank you Rik.

Sign in to comment.

Products


Release

R2015a

2 Answers

Answer by Rik
on 7 Sep 2019
Edited by Rik
on 7 Sep 2019
 Accepted Answer

This should do the trick:
X=2*(rand(5,5)-0.5);
Y=2*(rand(5,5)-0.5);
x=0.3;y=0.3;
d=point2pointcloud(X,Y,x,y)
function d=point2pointcloud(X,Y,x,y)
%X and Y are the coordinates of the point cloud
%x and y are the coordinates of the point
if ~isequal(size(X),size(Y)) || ~strcmp(class(X),class(Y))
error('X and Y must be same size and type')
end
if numel(x)~=1 || numel(y)~=1 || ~strcmp(class(x),class(y))
error('x and y must be scalar and same type')
end
if ~strcmp(class(x),class(X))
error('all inputs must be same type')
end
d=sqrt((X-x).^2+(Y-y).^2);
end
Edit:
To make the function easier to find, I'll paste it here. This function seems to return the required matrix.
function d=pointcloud_dist(X,Y)
%X and Y are the coordinates of the point cloud
if ~isequal(size(X),size(Y)) || ~strcmp(class(X),class(Y))
error('X and Y must be same size and type')
end
[ind1,ind2]=ndgrid(1:numel(X));
X1=X(ind1);X2=X(ind2);
Y1=Y(ind1);Y2=Y(ind2);
d=sqrt((X1-X2).^2+(Y1-Y2).^2);
end

  5 Comments

yes you are right,you suppose that we dont consider center. your code is worked . your code obtains a Symmetric matrix and the elemet of diagonal is zero(my target). but the matrix size is 25*25.
but I need 5*5 matrix(the first row shows the distance from point 1 from others(other four points), the second row shows the distance from point 2 to others,.....) so we will have 5 rows and five columns.
My input was 25 points, so if you don't provide my 25 randomly generated points but put in your 5 points instead, you will get a 5*5 matrix.
Thanks rik. I learned applicable information from you.
I did your suggestion about the release of matlab.
you answered all of my question, but one thing is wrong.
If you saw the picture of the points. They have equal distances(distance is contsant) but when we use "rand " it generates some random number with unequal distances.
Is there a code in matlab that generates random number with equal distance in (0 1)?
for example (0 , 0.1) , (0.1 , 0.1) , (0.2 , 0.1),(0.1 , 0),(0.1 , 0.2)
x=[0 0.1 0.2]
y=[0 0.1 0.2]

Sign in to comment.


Answer by Andrei Bobrov
on 7 Sep 2019
Edited by Andrei Bobrov
on 8 Sep 2019

X = rand(5,1)
Y = rand(5,1)
D = squareform(pdist([X,Y]))
or
XY = [X, Y];
D = sqrt(squeeze(sum((XY - permute(XY,[3,2,1])).^2,2)))
%For old MATLAB
D = sqrt(squeeze(sum(bsxfun(@minus,XY,permute(XY,[3,2,1])).^2,2)))

  1 Comment

Thanks andrei for your useful and brief answer.
Is there a code in matlab that generates random number with equal distance in (0 1)?
for example (0 , 0.1) , (0.1 , 0.1) , (0.2 , 0.1),(0.1 , 0),(0.1 , 0.2)
x=[0 0.1 0.2]
y=[0 0.1 0.2]

Sign in to comment.