HOW Calculate the distance of points form one center in 2-D space and display output in a distance matrix?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 8 Sep. 2019
Bearbeitet: Rik
am 8 Sep. 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)];
Akzeptierte Antwort
Rik
am 7 Sep. 2019
Bearbeitet: Rik
am 7 Sep. 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 Kommentare
Rik
am 7 Sep. 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.
Weitere Antworten (1)
Andrei Bobrov
am 7 Sep. 2019
Bearbeitet: Andrei Bobrov
am 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)))
Siehe auch
Kategorien
Mehr zu Point Cloud Processing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!