0

我有一个带有 N 62-D 向量的 Nx62 矩阵和一个带有向量标签的 NX1 向量。我试图用它们的标签来绘制这些向量,因为我想看到这些类在 62 维空间中绘制时的行为。根据前面引用的 NX1 向量的标签,这些向量属于三个类别。

如何在matlab中做到这一点?当我做 plot(vector,classes) 结果分析起来很奇怪,如何在图中放置标签?

我用来获取标签、向量和绘图的代码如下:

%labels is a vector with labels, vectors is a matrix where each line is a vector
[labels,vectors]=libsvmread('features-im1.txt');

当我绘制一个三维向量很简单

a=[1,2,3]
plot(a)

然后我得到结果

简单的情节

但现在我有一组向量和一组标签,我想查看它们的分布,我想绘制每个标签,但也想识别它们的类。如何在matlab中做到这一点?

编辑:这段代码几乎可以工作。问题在于,对于每个向量和类,绘图都会分配一种颜色。我只想要三种颜色和三个标签,每个班级一个。

[class,vector]=libsvmread('features-im1.txt'); 
%the plot doesn't allow negative and 0 values in the label 
class=class+2; 
labels = {'class -1','class 0','class 1'}; 
h = plot(vector); 
legend(h,labels{class})   
4

1 回答 1

1

如果我理解正确,这就是你想要的:

N = 5;
classes = [1 2 3 1 2]; % class of each vector. Size N x 1
colors = {'r', 'g', 'b'}; % you can also define them numerically
matrix = rand(N,62); % example data. Size N x 62
labels = {'class 1','class 2','class 3'}; % class names. Size max(classes) x 1
h = plot(matrix.');
h_first = NaN(1,3); % initialization
for k = 1:max(classes)
    ind = find(classes==k);
    set(h(ind), 'color', colors{k}) % setting color to all plots of a given class
    h_first(k) = h(ind(1)); % remember a handle of each color (for legend)
end
legend(h_first,labels)
于 2013-11-11T14:57:44.820 回答