我在這個問題上非常接近。我必須做的是過濾出一個單元陣列。單元格數組可以包含多種項目,但我想要做的是使用遞歸來抽出字符串。我非常接近這一個。當單元格中有空格時,我只是有一個問題。這是我應該得到:用遞歸過濾單元陣列
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