function cluster_N_plot(data,means,stds,method) % CLUSTER_N_PLOT - % % Inputs: % method: 1=> DON'T update starndard deviations, 2=> DO update standard deviations % % Written by: % -- % John L. Weatherwax 2005-08-14 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % % Iterative adjustment of clusters (must have a preconsieved % notion of what the cluster centers are): % %----- Niters = 10; figure; plot( data(:,1), data(:,2), 'o' ); axis( [ 0 7 -1 5 ] ); hold on; grid on; title( 'raw data' ); R = size(means,1); meansOld=means; for ni=1:Niters, plot( means(:,1), means(:,2), 'rx', 'MarkerSize', 10*ni ); pause(1); % Find the closest cluster for each datum: % clust=zeros(size(data,1),1); for di=1:size(data,1) yt=data(di,:); clust(di) = findNearestCluster( yt, means, stds ); end % Calculate new cluster centers/stardard deviations: % for ri=1:R, inds = find(clust==ri); means(ri,:) = mean(data(inds,:)); end switch method case 1 stds = ones(size(means)); case 2 for ri=1:R, inds = find(clust==ri); stds(ri,:) = std(data(inds,:)); end otherwise error( 'Unknown value of METHOD' ); end if( norm(meansOld-means) < 10^(-10) ) % We are done...exit break; else meansOld=means; end end plot( means(:,1), means(:,2), 'kx' );