Background
Expanding upon this question. I have a collection of points (in a three axes system, so with (x,y,z) coordinates), and i want to calculate the distance between each point.
For that i first have to make a matrix with all possible combinations of points (preferably without duplicates, to save on processing time), to then calculate all the distances.
Problem
Now I am trying to use meshgrid
for this, but it's getting pretty complicated. It's getting complicated because the (x,y,z) coordinates are in a matrix that is formatted as: pointCoordinates[x,y,z,pointnumber]
And i don't know how to tell meshgrid
to just combine point-1 and point-2 etc., without also combining all the separate x-coordinates with all the separate y-coordinates, etc. (wich are far too many combinations, most of which are useless).
Question
How do i keep meshgrid from making redundant combinations of coordinates? Or is there a simpler way to do this?
I guess i could reformat the pointCoordinates
matrix to a simple string array points
(with as many entries as there are coordinates). Where entry one is (1,3,5), entry two (2,4,2), etc. Thereby keeping the coordinates together, and limiting the number of possible combinations. But that seems redundant.
You can create 3 meshgrids, for
x
,y
andz
.Then compute the distance for each one:
Now, you need to extract the lower diagonal part, since there are duplications.
There is indeed a simpler way. If you only want to calculate non-redundant points, you can use pdist. Note that you can choose a different distance metric than Euclidean.
From the help: