Plot data sets with unique colors without using loop

My existing code which produces the desired results is as follows,
for i = 1:k
cluster = data(V==i,:);
plot(cluster(:,1),cluster(:,2),'x');
end
'data' is a 1000x2 double array containing x and y coordinates. V is a 1000x1 integer array with values between 1 to k i.e. there are k subsets of data in total
I need to plot the different subsets in different colors which matlab automatically assigns because of individual calls to plot.
Is there a better way to code this without using a loop?

回答(1 个)

That uses a color order, which is predefined and which will repeat. To get unique colors you'd have to pass in the color:
for i = 1:k
thisColor = rand(1, 3);
cluster = data(V==i,:);
plot(cluster(:,1), cluster(:,2), 'x', 'Color', thisColor, 'MarkerSize', 15);
end

类别

帮助中心File Exchange 中查找有关 Call Python from MATLAB 的更多信息

提问:

2017-12-4

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by