I'm working on this problem:
There is a 3d binary matrix which i cluster with the kmeans algorithm in matlab;
After that i get a value C which contains the coordinates of the centroids of these clusters in an array, for example:
C= 30.0000 15.0000 48.5000
100.2676 57.7382 80.7489
57.5000 85.0000 35.0000
27.5000 50.0000 69.5000
(4 centroids: first one with coordinates (30,15,48,5
))
where the rows represent the x,y
and z
coordinate of each centroid.
When I cluster another matrix I get a second value which also contains the coordinates of the second centroids. now i have to match the centroids from first and second clustering which have minimum distance and create an output where i can see which centroids belong together.
I have tried it with pdist(X)
and i get the distances pairwise of the coordinates but i can not match them together..
How to solve this, any idea?
You should use pdist2
instead of pdist
.
pdist2
calculates the pairwise distance between two sets of observations, whereas pdist
calculates the pairwise distance between all observations in a single matrix.
The documentation of pdist2
specifies the correct syntax and meaning of the output distance matrix:
D = pdist2(X,Y)
returns a matrix D containing the Euclidean distances between each pair of observations in the mx-by-n data matrix X and my-by-n data matrix Y. Rows of X and Y correspond to observations, columns correspond to variables. D is an mx-by-my matrix, with the (i,j) entry equal to distance between observation i in X and observation j in Y.
So, you should use this as follows:
D = pdist2(C1, C2);
Note 1: If you have find a function which almost does what you want (i.e. pdist
) it is often useful to look into the See Also
section of the manual and check if one of these may be helpful.