Рассмотрим следующий пример:
%# matrix of centroids
n = 5;
C = rand(2,n);
%# set up graphic objects
hScatter = gscatter(C(1,:), C(2,:), 1:n, [], [], 30); hold on
hQuiver = quiver(nan,nan,nan,nan);
set(hQuiver, 'AutoScale','off', 'Color','k')
axis([-10 10 -10 10])
drawnow, pause(1)
%# update and show C each iteration
for i=1:10
%# update centroids
oldC = C;
C = C + randn(size(C));
%# update centroids to new locations
set(hScatter, {'XData'},num2cell(C(1,:))', {'YData'},num2cell(C(2,:))')
%# plot arrow showing movement from old to new locations
set(hQuiver, 'XData',oldC(1,:), 'YData',oldC(2,:), ...
'UData',C(1,:)-oldC(1,:), 'VData',C(2,:)-oldC(2,:))
%# show iteration number
title( sprintf('Iteration %d',i) )
%# refresh plot
drawnow, pause(1)
end
![enter image description here](https://i.stack.imgur.com/7pYpZ.png)