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

1 次查看(过去 30 天)
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 个评论
Rik
Rik 2019-9-8
编辑:Rik 2019-9-8
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)];

请先登录,再进行评论。

采纳的回答

Rik
Rik 2019-9-7
编辑:Rik 2019-9-7
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 个评论
Rik
Rik 2019-9-7
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.
sona nil
sona nil 2019-9-8
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]

请先登录,再进行评论。

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2019-9-7
编辑:Andrei Bobrov 2019-9-8
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 个评论
sona nil
sona nil 2019-9-8
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]

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Point Cloud Processing 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by