2013-05-03 36 views
0

我有一個1x84單元陣列,我得到爲交叉驗證的索引:Matlab的:獲取單元陣列的元件從指數

indices = crossvalind('Kfold',length(filenames),k_fold); 
for i = 1:k_fold 
    test = (indices == i); 
    train = ~test; 

給定的測試和列車(84x1的1邏輯陣列或0)如何做我獲取通過測試/火車索引的所有文件名?

+2

'filenames(test)'和'filenames(train)'? – Dan 2013-05-03 12:56:05

+0

如果您的單元格數組超過了文件名{test}/filenames {train}(帶大括號) – 2013-05-03 13:37:17

回答

0

您可以對單元格數組應用邏輯索引來對其進行分片。這裏被簡化例如:

%# create a cell array of string 
C = cellstr(num2str((1:5)', 'file %d')); 

%# random split 
trainIdx = rand(size(C)) > 0.5; 
testIdx = ~trainIdx; 

%# slice cell array 
tr = C(trainIdx) 
ts = C(testIdx) 

注意,既trts是串本身的單元陣列。因此要訪問tr中的第一個字符串,您需要:

>> tr{1}