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

4 visualizaciones (últimos 30 días)
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 comentarios
Rik
Rik el 8 de Sept. de 2019
Editada: Rik el 8 de Sept. de 2019
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)];

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 7 de Sept. de 2019
Editada: Rik el 7 de Sept. de 2019
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 comentarios
Rik
Rik el 7 de Sept. de 2019
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 el 8 de Sept. de 2019
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]

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 7 de Sept. de 2019
Editada: Andrei Bobrov el 8 de Sept. de 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 comentario
sona nil
sona nil el 8 de Sept. de 2019
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]

Iniciar sesión para comentar.

Categorías

Más información sobre Point Cloud Processing en Help Center y File Exchange.

Productos


Versión

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by