Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
k-means clustering for 3D array

Subject: k-means clustering for 3D array

From: Subash

Date: 13 Dec, 2010 17:16:05

Message: 1 of 5

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

Subject: k-means clustering for 3D array

From: Sean de

Date: 13 Dec, 2010 17:39:21

Message: 2 of 5

"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

Subject: k-means clustering for 3D array

From: David Young

Date: 13 Dec, 2010 17:47:05

Message: 3 of 5

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.

Subject: k-means clustering for 3D array

From: Mina

Date: 30 Apr, 2013 14:57:10

Message: 4 of 5

I was wondering if you found the solution to your problem for 3D data clustering. I have the same issue.

Subject: k-means clustering for 3D array

From: Rob Dupre

Date: 23 Oct, 2013 09:09:08

Message: 5 of 5

"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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us