MATLAB: K means

k-means

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.

Best Answer

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.