2014-10-18 93 views
0

我有2台车的数据,AB大小不同,我想用于训练分类的,我有2 焦炭变量,如2个标签,的分类标签数据在Matlab

L1 = 'label A'; 
L2 = 'label B'; 

如何生成适当的标签?

我会先用cat(1,A,B);来合并数据。

根据size(A,1)size(B,1),它应该是这样的,

label = ['label A' 
     'label A' 
     'label A' 
     . 
     . 
     'label B' 
     'label B']; 

回答

1

假设以下几点:

na = size(A,1); 
nb = size(B,1); 

下面是创建Ia的细胞阵列的几个方法贝尔:

  1. repmat

    labels = [repmat({'label A'},na,1); repmat({'label B'},nb,1)]; 
    
  2. 细胞阵列填充

    labels = cell(na+nb,1); 
    labels(1:na)  = {'label A'}; 
    labels(na+1:end) = {'label B'}; 
    
  3. 细胞阵列线性索引

    labels = {'label A'; 'label B'}; 
    labels = labels([1*ones(na,1); 2*ones(nb,1)]); 
    
  4. 细胞阵列线性indexin克(另)

    idx = zeros(na+nb,1); idx(nb-1)=1; idx = cumsum(idx)+1; 
    labels = {'label A'; 'label B'}; 
    labels = labels(idx); 
    
  5. num2str

    labels = cellstr(num2str((1:(na+nb) > na).' + 'A', 'label %c')); 
    
  6. strcat的

    idx = [1*ones(na,1); 2*ones(nb,1)]; 
    labels = strcat({'label '}, char(idx+'A'-1)); 
    

...你的想法:)


注意,它总是容易的字符串单元阵列和炭基体之间进行转换:

% from cell-array of strings to a char matrix 
clabels = char(labels); 

% from a char matrix to a cell-array of strings 
labels = cellstr(clabels); 
+0

让我们看看别人是否能想到更多的方法:) – Amro 2014-10-18 14:47:58

+1

有点疯狂,但也有可能是'labels = cell(na + nb,1);''[labels {1:na}]] = deal('label A');'和'[labels {na + 1:end}] = deal('label B');'。 – MeMyselfAndI 2014-10-19 11:38:20

+0

@JandeGier:一点都不疯狂,展示交易功能的好方法 – Amro 2014-10-19 12:18:34

0
label = [repmat('label A',size(A,1),1); repmat('label B',size(B,1),1) ]; 

这将创建一个你正在寻找的标签矩阵。您需要使用repmat。 Repmat帮助您重复某个值多次

+2

这是密切,只需12秒).. – MeMyselfAndI 2014-10-18 13:40:10

1

如果标签名称具有相同的长度,你创建一个数组,像这样:

L = [repmat(L1,size(A,1),1);repmat(L2,size(B,1),1)]; 

否则,你需要使用电池阵列:

L = [repmat({L1},size(A,1),1);repmat({L2},size(B,1),1)]; 
+0

谢谢,我知道这将不会那么难,我只是找不到复制_char_的方法,但正如你所说'repmat'就是答案。 – Rashid 2014-10-18 13:50:08

+0

我知道了.. – lakesh 2014-10-18 13:56:45