我想strfind
函数上使用cellfun
函数来查找另一个字符串单元格数组中字符串单元格数组中每个字符串的索引,以将它们排除在外。
strings = {'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii','jjj'};
excludedStrings = {'b','g','h'};
idx = cellfun('strfind',strings,excludedStrings);
idx = cell2mat = idx;
idx = reshap(idx,numel(idx),1);
idx = unique(idx);
strings(cell2mat(idx)) = [];
cellfun
呼叫线路有错误,如何解决?
这是一个可爱的单行:
strings = regexprep(strings, excludedStrings, '');
故障:
- 所有要搜索的单词/字符都将传递给
regexprep
- 此函数将上面给出的集合中任何单词/字符的每次出现替换为空字符串 (
''
)。
它将自动对单元格数组string
中的所有元素重复此操作。
如果您还希望从单元格string
中删除任何空字符串,请在上述命令之后执行此操作:
strings = strings(~cellfun('isempty', strings));
我认为你在追求这个:
idx = cellfun(@(str) any(cellfun(@(pat) any(strfind(str,pat)),excludedStrings)),strings)
idx =
0 1 0 0 0 0 1 1 0 0
之后,您当然可以申请:
strings(idx) = [];
因为您要交叉检查两个单元格数组(其中一个是数组),所以您需要嵌套两个cellfun
。