2014-10-18 96 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