0

I have the following piece of code:

function label = kmeans(X, k)
% Perform k-means clustering.
%   X: d x n data matrix
%   k: number of seeds
% Written by Michael Chen (sth4nth@gmail.com).
n = size(X,2);
last = 0;
label = ceil(k*rand(1,n));  % random initialization
while any(label ~= last)
    [u,~,label] = unique(label);   % remove empty clusters
    k = length(u);
    E = sparse(1:n,label,1,n,k,n);  % transform label into indicator matrix
    m = X*(E*spdiags(1./sum(E,1)',0,k,k));    % compute m of each cluster
    last = label;
    [~,label] = max(bsxfun(@minus,m'*X,dot(m,m,1)'/2),[],1); % assign samples to the nearest centers
end
[~,~,label] = unique(label);

I would like to convert it to Mathematica, but I am not sure how to handle the matrix operations such as spdiags annd bsxfun in Mathematica...

For the moment, I have:

KMEANS[data0_, clusters0_] := 
 Module[{data = data0, clusters = clusters0},
  n = Length[data];
  last = 0;
  label = RandomInteger[clusters, n];
  While[MatrixQ[label - last, PossibleZeroQ] != True,
   e = SparseArray[ConstantArray[0, {n, k}]];
   SET[index0_] := Module[{index = index0},
     e[[index[[1]], index[[2]]]] = 1;
     ];
   Map[SET, label]

   ]
  ]

How could this be done nicely?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
user13675
  • 947
  • 1
  • 6
  • 15

0 Answers0