Applying k means to matrix

I have the result after dividing same pixels into blocks,
result is in variable out in workspace
27 [1,9]
30 [1,12]
31 [1,13]
32 [1,14]
33 [1,15]
34 [1,16]
35 [1,17]
36 [1,18]
37 [1,19]
38 [1,20]
39 [1,21]
now i have to apply k means for the above result ,i.e for each block i have to apply k means
please help

11 Comments

Okay, go ahead. The function name is kmeans()
walter i tried
z=out(:,2);
z2=cell2mat(z);
for i=1:length(z2)
f=kmeans(2,z2(i));
end
i get error
Error using ==> kmeans at 283
X must have more rows than the number of
clusters.
that y i posted these question since i got error
You are overwriting "f" on each iteration of the loop.
What do the arguments "2" and "z2(i)" represent to you?
Pat
Pat on 27 Nov 2012
Edited: Walter Roberson on 27 Nov 2012
walter i want to apply for all rows,in this 1st column represents pixel value and next column represents pixel position
now please tell how to apply k means for each block,i dont have idea for applying to each block the previous code i used for separating into blocks is
S is my image of 256x256;
d = unique(S);
out = [];
for jj = 1:numel(d)
s=regionprops(bwconncomp(S == d(jj),4),'PixelList');
out = [out;[repmat({d(jj)},numel(s),1) {s.PixelList}']];
end
[ii,ii]=sortrows(cell2mat(cellfun(@(x)x(1,:),out(:,2),'un',0)),[2 1]);
out = out(ii,:);
now for out i have to apply kmeans,i.e for each block
Pat
Pat on 27 Nov 2012
please look at this
my output is like that ,i have to apply kmeans for each block,please assist
I don't understand what that code is intended to do, and I do not understand how you ended up (in your file) with multiple entries with the same pixel value.
Anyhow, let us stick for the moment with the question of what kmeans(2,z2(i)) means to you. Why is the first argument always 2? Why is the second argument variable? Which argument represents the number of clusters? Which argument represents the data to be clustered?
Pat
Pat on 27 Nov 2012
z2(i) represents each row,applying k as 2,second argument is data ,so it is every row which i represents,number of clusters is 2
Time for you to re-read the documentation for kmeans to see what arguments it expects to be passed in.
Pat
Pat on 27 Nov 2012
Walter i have read the documentation and have understood ,but no idea how to implement after diniding into blocks ,I know to apply for image or matrix ,but to the output below i dont know how to apply
Okay, so what is your current version of the code (after taking the documentation into account) ?
Walter i tried
assuminug my values are
S=27 [1,9]
30 [1,12]
31 [1,13]
32 [1,14]
33 [1,15]
34 [1,16]
35 [1,17]
36 [1,18]
37 [1,19]
38 [1,20]
39 [1,21]
S=S(:,2);
S=cell2mat(S);
for i=1:length(S)
D=kmeans(S(i(:)),2);
r=reshape(D,1,[]);
end
dont know whether it is correct or not ,and i saw a reference code in
http://www.mathworks.in/matlabcentral/answers/46705-how-images-are-divided-into-sub-images
here the peppers image is divided into many parts,like that i want ti divide image ,such the each subimage has same pixel value,and then apply k means for each block

Sign in to comment.

Answers (0)

Tags

Asked:

Pat
on 27 Nov 2012

Community Treasure Hunt

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

Start Hunting!