0

I have 2 sets of train data, A and B with different sizes, which I want to use for training the classifier, and I have 2 labels in 2 char variables like,

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

How can I produce appropriate labels ?

I will use cat(1,A,B); to merge data first.

Depending on the size(A,1) and size(B,1), It should be something like,

label = ['label A'
         'label A'
         'label A' 
         .
         .
         'label B'
         'label B'];
Amro
  • 123,847
  • 25
  • 243
  • 454
Rashid
  • 4,326
  • 2
  • 29
  • 54

3 Answers3

1

If the label names have the same length, you create an array like so:

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

Otherwise, you need to use a cell array:

L = [repmat({L1},size(A,1),1);repmat({L2},size(B,1),1)];
MeMyselfAndI
  • 1,320
  • 7
  • 12
  • Thanks, I knew it wouldn't be so hard, I just couldn't find a way to replicate _char_, but as you said `repmat` is the answer. – Rashid Oct 18 '14 at 13:50
1

Assuming the following:

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

Here are a few ways to create the cell-array of labels:

  1. repmat

    labels = [repmat({'label A'},na,1); repmat({'label B'},nb,1)];
    
  2. cell-array filling

    labels = cell(na+nb,1);
    labels(1:na)     = {'label A'};
    labels(na+1:end) = {'label B'};
    
  3. cell-array linear indexing

    labels = {'label A'; 'label B'};
    labels = labels([1*ones(na,1); 2*ones(nb,1)]);
    
  4. cell-array linear indexing (another)

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

... you get the idea :)


Note that it's always easy to convert between a cell-array of strings and a char-matrix:

% 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);
Amro
  • 123,847
  • 25
  • 243
  • 454
0
label = [repmat('label A',size(A,1),1); repmat('label B',size(B,1),1) ];

This will create the label matrix you are looking for. You need to use repmat. Repmat helps you to repeat a certain value many times

lakshmen
  • 28,346
  • 66
  • 178
  • 276