关于函数strfind的matlab cellfun

时间:2012-11-08 12:35:11

标签: matlab matrix cell

我想在cellfun函数上使用strfind函数来查找另一个字符串数组中字符串的单元格数组中每个字符串的索引,以将其排除在外。

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致电热线有错误,我该如何解决?

2 个答案:

答案 0 :(得分:3)

这是一个可爱的单行:

strings = regexprep(strings, excludedStrings, '');

故障:

  • 要搜索的所有字词/字符都会传递到regexprep
  • 此函数用空字符串('')替换上面给出的集合中任何字/字符的每次出现。

它会自动对单元格数组string中的所有元素重复此操作。

如果您还希望从单元格string中删除任何空字符串,请在上述命令后执行此操作:

strings = strings(~cellfun('isempty', strings));

答案 1 :(得分:2)

我想你是在这之后:

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

相关问题