A component from Kmeans algorithm

1 view (last 30 days)
hello,
i am a new user of matlab. i am trying to understand kmeans algotirhm. kmeans function calls this function d=DistMatrix(m,c); where m is a two dimensiona vector and c are points chossen from m as center
function d=DistMatrix(A,B)
[hA,wA]=size(A);
[hB,wB]=size(B);
if wA ~= wB, error(' second dimension of A and B must be the same'); end
for k=1:wA
C{k}= repmat(A(:,k),1,hB);
D{k}= repmat(B(:,k),1,hA);
end
S=zeros(hA,hB);
for k=1:wA
S=S+(C{k}-D{k}').^2;
end
d=sqrt(S);
i canot understand working of this code specifically the allocations performed in first for loop. can sombody help me?

Accepted Answer

Walter Roberson
Walter Roberson on 7 Feb 2011
This is effectively a euclidean distance calculation.
C{k} = repmat(A(:,k),1,hB)
takes the k'th column of A and repeats it to form a matrix with hB columns all the same as that one column. hB is the number of rows in B.
D{k} = repmat(B(:,k),1,hA)
takes the k'th column of B and repeats it to form a matrix with hA columns all the same as that one column. hA is the number or rows in A.
One thing that would not be immediately apparent to you as a new user of Matlab is that the expression C{k}-D{k}' involves taking the complex conjugate transpose of D{k} and subtracting that from C{k} .
It is not immediately clear to me that the conjugate transpose is the proper thing to use there instead of the regular transpose, but I would need to work through it in more detail to be decide that point.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!