Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

k-means clustering for 3D array

2,099 views
Skip to first unread message

Subash

unread,
Dec 13, 2010, 12:16:05 PM12/13/10
to
I have a 3 dimensional array of data and I want to cluster them in to 2 groups (k=2), most matlab source codes found in web are working with 2 dimensional array (including kmeans), How do I do kmeans clustering for 3D data. Any code available? Any Idea?
Thanks
Subash

Sean de

unread,
Dec 13, 2010, 12:39:21 PM12/13/10
to
"Subash " <subb...@yahoo.com> wrote in message <ie5kcl$ki6$1...@fred.mathworks.com>...

> I have a 3 dimensional array of data and I want to cluster them in to 2 groups (k=2), most matlab source codes found in web are working with 2 dimensional array (including kmeans), How do I do kmeans clustering for 3D data. Any code available? Any Idea?
> Thanks
> Subash

I seem to recall this one working in 3D:
http://www.mathworks.com/matlabcentral/fileexchange/24616-kmeans-clustering

David Young

unread,
Dec 13, 2010, 12:47:05 PM12/13/10
to
The k-means algorithm operates on a collection of vectors. This is naturally structured as a 2-D matrix, with, for example, one vector in each row. Then X(p,q) contains the q'th element of the p'th vector. This is a property of the algorithm, not of any particular implementation.

If you have a 3-D matrix, I'm guessing that two of the indices can be combined to give a single index which selects a vector, and the third is the index into the vector. For example, if the data are vector measured at each pixel of an image, then often the first two indices select the pixel and the third selects an element of the vector. Then you could do something like this to conflate the first two indices:

[m, n, d] = size(imagedata);
N = m*n;
X = reshape(imagedata, N, d);

Then after calling your kmeans function you'll need another reshape to get the 1-D vector of cluster assignments back to 2-D.

You'll have to work out the details for your particular case though.

Mina

unread,
Apr 30, 2013, 10:57:10 AM4/30/13
to
I was wondering if you found the solution to your problem for 3D data clustering. I have the same issue.

Rob Dupre

unread,
Oct 23, 2013, 5:09:08 AM10/23/13
to
"Mina" wrote in message <klom45$muv$1...@newscl01ah.mathworks.com>...
> I was wondering if you found the solution to your problem for 3D data clustering. I have the same issue.

I have just been working with the same issue.

Im not sure if my solution will help but here goes...
I had a 3d matrix (PxQxR) in my case 50x50x50 logical.
I then created from this a 125000x4 which held the element location in x,y,z and then its value.
eg.
1 1 1 0
1 1 2 0
1 1 3 0
....
I created this with a nested for loop to go over my 50x50x50 array but im sure there is a much better way of doing so! I have only been using matlab for about 2 weeks.Youll have to bare with me.

using this new matrix passed to Matlabs built in kmeans function
[cidx, ctrs] = kmeans(new_matrix(:,1:3), num_centroids);

And then i plotted them using plot3
plot3(new_matrix(cidx==1,1),new_matrix(cidx==1,2),new_matrix(cidx==1,3),'b.');
hold on ; plot3(new_matrix(cidx==2,1),new_matrix(cidx==2,2),new_matrix(cidx==2,3),'g.');
hold on ; plot3(new_matrix(cidx==3,1),new_matrix(cidx==3,2),new_matrix(cidx==3,3),'r.');
hold on ; plot3(new_matrix(cidx==4,1),new_matrix(cidx==4,2),new_matrix(cidx==4,3),'w.');
hold on ; plot3( ctrs(:,1),ctrs(:,2),ctrs(:,3),'kx');

Hope this helps
0 new messages