3

我想在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我该如何解决?

4

2 回答 2

3

这是一个可爱的单行:

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

分解:

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

它将自动对 cell-array 中的所有元素重复此操作string

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

strings = strings(~cellfun('isempty', strings));
于 2012-11-08T12:55:09.687 回答
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) = [];

因为您有两个要交叉检查的单元格数组(其中一个是数组),所以您需要嵌套两个cellfuns。

于 2012-11-08T13:28:47.093 回答