# Converting code from Matlab to Mathematica [duplicate]

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[], index[]]] = 1;
];
Map[SET, label]

]
]


How could this be done nicely?

• I wrote an implementation of k-means in this answer. – rm -rf Sep 20 '14 at 21:43
• k-means is also one of the options in the ClusteringComponents function. – bill s Sep 20 '14 at 22:02
• Since people here are much more likely to be familiar with Mathematica functions than MATLAB ones I think you should include specific descriptions of what the MATLAB functions do that you wish to implement in Mathematica. – Mr.Wizard Sep 21 '14 at 8:56
• spdiags extracts all the non-zero diagonals from a matrix. – dr.blochwave Sep 21 '14 at 10:07
• bsxfun performs element-wise operations. e.g. in your example, bsxfun(@minus,m'*X,dot(m,m,1)'/2) will subtract the result of dot(m,m,1)'/2 from m'*X. – dr.blochwave Sep 21 '14 at 10:09