Replace function

Hello, I have the following function,
function label = litekmeans(X, k) n = size(X,2); last = 0; label = ceil(k*rand(1,n)); % random initialization while any(label ~= last) [~,~,label] = unique(label); % remove empty clusters E = sparse(1:n,label,1,n,k,n); % transform label into indicator matrix center = X*(E*spdiags(1./sum(E,1)',0,k,k)); % compute center of each cluster last = label; [~,label] = max(bsxfun(@minus,center'*X,0.5*sum(center.^2,1)')); % assign samples to the nearest centers end
I have to replace the 'unique' and the 'spdiags' function. I am allowed to use only built in functions.
I am quite new in matlab and i can't understand how those 2 functions work here.
Can anyone explain me what are they actually doing or how they work.
I will need to understand how they work so be able to replace them.
Can anyone help me? Thanks nonyt!

3 Comments

nonyt
nonyt on 31 Mar 2011
sorry the function does not appear very well here it is again:
function label = litekmeans(X, k)
n = size(X,2);
last = 0;
label = ceil(k*rand(1,n)); % random initialization
while any(label ~= last)
[~,~,label] = unique(label); % remove empty clusters
E = sparse(1:n,label,1,n,k,n); % transform label into indicator matrix
center = X*(E*spdiags(1./sum(E,1)',0,k,k)); % compute center of each cluster
last = label;
[~,label] = max(bsxfun(@minus,center'*X,0.5*sum(center.^2,1)')); % assign samples to the nearest centers
end
Andrew Newell
Andrew Newell on 31 Mar 2011
Have you tried "doc unique"?
Matt Fig
Matt Fig on 31 Mar 2011
First off, please go back and use the {} Code button after highlighting the code you pasted into the window, then delete your comment where you pasted it again.
Second, you can learn exactly what goes on with the UNIQUE function by typing:
edit unique
the reading the function.

Sign in to comment.

Answers (0)

Asked:

on 31 Mar 2011

Community Treasure Hunt

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

Start Hunting!