In Matlab, what's the fastest way to assign different label values when plotting

95 views Asked by At

I currently have a vectors of labels, Y, that describes each column of my data matrix X. For instance, if Y=1, then X is of the first class. I'm trying to plot X as a different colour (or shape) based on the value of Y. Is there a faster way of doing this than my code below? I'm currently using a for loop which takes a while when I'm using lots of data.

n = size(Y);
for i = 1:n(2)
    if Y(i) == 1
        colour = 'y';
        shape = 'c';
    elseif Y(i) == 2
        colour = 'm';
        shape = 'c';
    elseif Y(i) == 3
        colour = 'c';
        shape = 'c';
    elseif Y(i) == 4
        colour = 'r';
        shape = 'c';
    elseif Y(i) == 5
        colour = 'g';
        shape = 'c';
    elseif Y(i) == 6
        colour = 'b';
        shape = 'c';
    elseif Y(i) == 7
        colour = 'k';
        shape = 'c';
    elseif Y(i) == 8
        colour = 'r';
        shape = 's';
    elseif Y(i) == 9
        colour = 'b';
        shape = 's';
    elseif Y(i) == 10
        colour = 'g';
        shape = 's';
    end
    subplot
    scatter(X(1,i), X(2,i), 12, colour, shape, 'filled')
    hold on
end
1

There are 1 answers

2
am304 On BEST ANSWER

You could get rid of all your if/elseif loops:

n = size(Y);
colour = {'y','m','c','r','g','b','k','r','b','g'};
shape = {'c','c','c','c','c','c','c','s','s','s'};
for ii = 1:n(2)
   subplot
   scatter(X(1,ii), X(2,ii), 12, colour{Y(ii)}, shape{Y(ii)}, 'filled')
   hold on
end