【发布时间】:2015-05-26 17:03:14
【问题描述】:
我非常接近这个问题。我要做的是过滤掉一个单元格数组。单元格数组中可以包含各种项目,但我想要做的是使用递归提取字符串。我非常接近这一点。当单元格中有空格时,我只是有一个问题。这是我应该得到的:
Test Cases:
cA1 = {'This' {{{[1:5] true} {' '}} {'is '} false true} 'an example.'};
[filtered1] = stringFilter(cA1)
filtered1 => 'This is an example.'
cA2 = {{{{'I told '} 5:25 'her she'} {} [] [] ' knows'} '/take aim and reload'};
[filtered2] = stringFilter(cA2)
filtered2 => 'I told her she knows/take aim and reload'
这是我所拥有的:
%find the strings in the cArr and then concatenate them.
function [Str] = stringFilter(in)
Str = [];
for i = 1:length(in)
%The base case is a single cell
if length(in) == 1
Str = ischar(in{:,:});
%if the length>1 than go through each cell and find the strings.
else
str = stringFilter(in(1:end-1));
if ischar(in{i})
Str = [Str in{i}];
elseif iscell(in{i})
str1 = stringFilter(in{i}(1:end-1));
Str = [Str str1];
end
end
end
end
我尝试使用“ismember”,但没有奏效。有什么建议么?我的代码输出如下:
filtered1 => 'This an example.'
filtered2 => '/take aim and reload'
【问题讨论】:
标签: arrays matlab recursion filtering cell-array