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:
edit function

Subject: edit function

From: nonyt

Date: 31 Mar, 2011 21:28:05

Message: 1 of 2

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 need to rewrite that with out using the 'unique' and with out using the 'spdiags'..

Actually I can use only build in functions.

I am quite new in matlab and i can't understand what these 2 functions, that i need to remove, doing there...
Can anyone explain me how this 2 functions work in the litekmeans function. So I will be able to implement my own code to do what they do...?
Or any other help is welcome...

Regards

Subject: edit function

From: Florin Neacsu

Date: 31 Mar, 2011 21:56:04

Message: 2 of 2

"nonyt" wrote in message <in2rl5$rb5$1@fred.mathworks.com>...
> 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 need to rewrite that with out using the 'unique' and with out using the 'spdiags'..
>
> Actually I can use only build in functions.
>
> I am quite new in matlab and i can't understand what these 2 functions, that i need to remove, doing there...
> Can anyone explain me how this 2 functions work in the litekmeans function. So I will be able to implement my own code to do what they do...?
> Or any other help is welcome...
>
> Regards

Hi,

Both unique and spdiags are built in function. Try typing doc unique and doc spdiags. You will see some documentation on what these functions do.

Although is possible to write you own code in order to replace them I think you should pay a great deal of attention to spdiags. There are more than 1 way to implement sparse matrices and you should be sure you are using the same as Matlab.

May I ask why you want to write your own code for these functions ? They are not part of any toolbox ...

Regards,
Florin

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