简体   繁体   English

Matlab:从索引获取单元格数组的元素

[英]Matlab: Get elements of Cell Array from Indices

I have a 1x84 cell array that I get the indices for cross validation: 我有一个1x84的单元格数组,可以获取用于交叉验证的索引:

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

Given test and train (84x1 logical arrays of 1 or 0) how do I get all filenames that are indexed by test/train? 给定测试和训练(1或0的84x1逻辑数组),我如何获得所有通过测试/训练索引的文件名?

You could apply logical indexing on the cell array to slice it. 您可以在单元格数组上应用逻辑索引以对其进行切片。 Here is simplified example: 这是简化的示例:

%# 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)

Note that both tr and ts are cell arrays of string themselves. 请注意, trts都是字符串本身的单元格数组。 So to access the first string in tr , you do: 因此,要访问tr的第一个字符串,您需要执行以下操作:

>> tr{1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM