k means

4 views (last 30 days)
Mahalakshmi
Mahalakshmi on 27 Jun 2011
I'm trying to perform k means on a 512*512 img of class uint8. Here is my code ab = double(img(:,:)); nrows = 512; ncols = 512; ab = reshape(ab,nrows*ncols,1); [cluster_idx cluster_center] = kmeans(ab,[],'distance','sqEuclidean',... 'emptyaction','Singleton','start',[1,2;2,1]); but this is the error msg I get ??? Subscripted assignment dimension mismatch.
Error in ==> kmeans>batchUpdate at 428 [C(changed,:), m(changed)] = gcentroids(X, idx, changed, distance);
Error in ==> kmeans at 337 converged = batchUpdate(); Please help me figure out where I am going wrong. Thanx in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Jun 2011
Note that your setup code is equivalent to
ab = double(img(:));
You are passing in a single column of data to kmeans(), so kmeans is getting 1D data, but you are trying to initialize the centroids as a pair of two-dimensional points.
  3 Comments
Walter Roberson
Walter Roberson on 27 Jun 2011
Yes, you can perform kmeans on grayscale images using the inbuilt kmeans function. You must, however, first define what it is you want to cluster on.
Looking through the documentation for kmeans for the version I have and for the latest (2011a) version, I do not see any possibility of using an empty matrix in place of the number of clusters ? Which version are you using?
Mahalakshmi
Mahalakshmi on 27 Jun 2011
I too use the same version. When I changed the distance measure to cosine or correlation ,this is the error I get..
Error using ==> kmeans at 157
Some points have small relative magnitudes, making them effectively zero.
Either remove those points, or choose a distance other than 'cosine'.

Sign in to comment.

More Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!