在Matlab中,绘制时分配不同标签值的最快方法是什么

时间:2014-11-14 16:53:32

标签: matlab label scatter

我目前有一个标签向量Y,它描述了我的数据矩阵X的每一列。例如,如果Y = 1,则X是第一类。我试图根据Y的值将X绘制为不同的颜色(或形状)。有没有比下面的代码更快的方法呢?我目前正在使用for循环,当我使用大量数据时需要一段时间。

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 个答案:

答案 0 :(得分:2)

你可以摆脱所有if/elseif循环:

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