COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### k-means clustering for 3D array

• Email
• Follow

```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
```
 0
Reply Subash 12/13/2010 5:16:05 PM

See related articles to this posting

```"Subash " <subbaash@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
```
 0
Reply Sean 12/13/2010 5:39:21 PM

```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.
```
 0
Reply David 12/13/2010 5:47:05 PM

```I was wondering if you found the solution to your problem for 3D data clustering. I have the same issue.
```
 0
Reply mjafarpoor (3) 4/30/2013 2:57:10 PM

```"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
Reply k0915251 (3) 10/23/2013 9:09:08 AM

4 Replies
1004 Views

Similar Articles

12/6/2013 7:25:44 PM
[PageSpeed]