【问题标题】:Label Data for Classification in Matlab在 Matlab 中为分类标记数据
【发布时间】:2014-10-18 22:51:13
【问题描述】:

我有 2 组训练数据集,AB 大小不同,我想用它们来训练分类器,我在 2 个 char 变量中有 2 个标签,例如,

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'];

【问题讨论】:

    标签: string matlab matrix char cell-array


    【解决方案1】:

    假设如下:

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

    以下是创建标签元胞数组的几种方法:

    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. 元胞数组线性索引(另一种)

      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);
    

    【讨论】:

    • 让我们看看其他人能不能想出更多办法:)
    • 有点疯狂,但也有可能是labels = cell(na+nb,1);[labels{1:na}] = deal('label A');[labels{na+1:end}] = deal('label B');
    • @JandeGier:一点也不疯狂,展示deal功能的好方法
    【解决方案2】:

    如果标签名称的长度相同,则创建一个数组,如下所示:

    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)];
    

    【讨论】:

    • 谢谢,我知道这不会那么难,我只是找不到复制 char 的方法,但正如你所说,repmat 就是答案。
    【解决方案3】:
    label = [repmat('label A',size(A,1),1); repmat('label B',size(B,1),1) ];
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多