>type kmeanscluster function kmeanscluster (x: numerical, k: index) n=rows(x); m=cols(x); i=floor((0:k)/k*(n-1))+1; means=zeros(k,m); loop 1 to k; means[#]=sum(x[i[#]:(i[#+1]-1)]')'/(i[#+1]-i[#]); end; j=1:n; loop 1 to n; d=sum((x[#]-means)^2); j[#]=extrema(d')[2]; end; repeat loop 1 to k; i=nonzeros(j==#); if cols(i)==0 then means[#]=1; else means[#]=(sum(x[i]')/cols(i))'; endif; end; jold=j; loop 1 to n; d=sum((x[#]-means)^2); j[#]=extrema(d')[2]; end; if all(jold==j) then break; endif; end return j endfunction