我在这个问题上非常接近。我必须做的是过滤出一个单元阵列。单元格数组可以包含多种项目,但我想要做的是使用递归来抽出字符串。我非常接近这一个。当单元格中有空格时,我只是有一个问题。这是我应该得到:用递归过滤单元阵列
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'
这个好工作! – Divakar 2014-10-31 14:51:09
好吧,这真的有道理:)谢谢! – 2014-10-31 16:06:26