-5

I have a vector of points in a 3D space.

A = rand(80,3)

I want a vector containing all the distances between the points.

D = pdist(A,'euclidean') returns a vector 1-by-3160 could you please tell me if it is the right function to use? thanks

4

1 回答 1

2

If you don't have the statistics toolbox, it is not the correct function to use. If you do - then it is one of many functions you could use. You can also do something like:

sz = size(A);
A1 = reshape(A, [1 sz]);
A2 = permute(A1, [2 1 3]);
D = sqrt(sum(bsxfun(@minus, A1, A2).^2,3));

This calculates the distance between any two points explicitly (thus, does twice as much work, and takes over twice as much space: 6400 instead of 3180 elements). However, it's easier to look up the distance between any two points.

You can then compare the result of this calculation with the one you get from pdist, and convince yourself whether it's the same. Be careful - pdist returns a vector by default, so you need to look at the documentation to make sure you understand how to interpret it. The function squareform can help you:

difference = sum(abs(squareform(D)) - pdist(A, 'euclidean'));

It depends on what you want... and what your definition of "the right function" is...

于 2013-08-30T17:48:58.383 回答