I would iterate through all the clusters and do what you already written, and use the hold on
option to accumulate all the plots in the same plot. Something like this:
% Generate three clouds of points in 2D:
c1 = bsxfun(@plus, 0.5 * randn(50,2), [1 3]);
c2 = bsxfun(@plus, 0.6 * randn(20,2), [0 0]);
c3 = bsxfun(@plus, 0.4 * randn(20,2), [1 1]);
data = [c1, ones(50,1); ...
c2, 2*ones(20,1); ...
c3, 3*ones(20,1)];
% Plot the data points with different colors
clf
plot(c1(:,1), c1(:,2),'r+', 'LineWidth', 2);
hold on
plot(c2(:,1), c2(:,2),'k+', 'LineWidth', 2);
plot(c3(:,1), c3(:,2),'b+', 'LineWidth', 2);
x = data(:,1);
y = data(:,2);
cluster = data(:,3);
clusters = unique(cluster);
for i = 1:length(clusters)
px = x(cluster == clusters(i));
py = y(cluster == clusters(i));
if length(px) > 2
k = convhull(px, py);
plot(px(k), py(k), '-');
end
end
It gives the following result: