Asked by sona nil
on 7 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)

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

sona nil
on 7 Sep 2019

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.

Rik
on 7 Sep 2019

sona nil
on 8 Sep 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]

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)))

Opportunities for recent engineering grads.

Apply Today
## 8 Comments

## Rik (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743129

## sona nil (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743132

## Rik (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743134

## Rik (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743240

## Rik (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743466

## sona nil (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743476

## Rik (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743502

## sona nil (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/479279-how-calculate-the-distance-of-points-form-one-center-in-2-d-space-and-display-output-in-a-distance#comment_743583

Sign in to comment.