Arrays 用递归过滤单元数组
我很接近这个问题。我要做的是过滤掉一个单元格数组。单元格数组中可以有多种项目,但我想做的是使用递归拉出字符串。这一点我很接近。当单元格中有空格时,我就遇到了一个问题。这是我应该得到的:Arrays 用递归过滤单元数组,arrays,matlab,recursion,filtering,cell-array,Arrays,Matlab,Recursion,Filtering,Cell Array,我很接近这个问题。我要做的是过滤掉一个单元格数组。单元格数组中可以有多种项目,但我想做的是使用递归拉出字符串。这一点我很接近。当单元格中有空格时,我就遇到了一个问题。这是我应该得到的: Test Cases: cA1 = {'This' {{{[1:5] true} {' '}} {'is '} false true} 'an example.'}; [filtered1] = stringFilter(cA1) filtered1 =>
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'
您可以将函数简化为
function [Str] = stringFilter(in)
Str = [];
for i = 1:length(in)
if ischar(in{i})
Str = [Str in{i}];
elseif iscell(in{i})
str1 = stringFilter(in{i});
Str = [Str str1];
end
end
end
只要在测试中遍历单元格中的所有元素,不管它是字符串还是单元格。在后者中,再次调用此单元格的函数。输出:
>> [filtered1] = stringFilter(cA1)
filtered1 =
This is an example.
>> [filtered2] = stringFilter(cA2)
filtered2 =
I told her she knows/take aim and reload
您可以将函数简化为
function [Str] = stringFilter(in)
Str = [];
for i = 1:length(in)
if ischar(in{i})
Str = [Str in{i}];
elseif iscell(in{i})
str1 = stringFilter(in{i});
Str = [Str str1];
end
end
end
只要在测试中遍历单元格中的所有元素,不管它是字符串还是单元格。在后者中,再次调用此单元格的函数。输出:
>> [filtered1] = stringFilter(cA1)
filtered1 =
This is an example.
>> [filtered2] = stringFilter(cA2)
filtered2 =
I told her she knows/take aim and reload
线路
Str = ischar(in{:,:});
这就是问题所在。这对我来说毫无意义
你很快就能得到答案,但犯了一些重大但很小的错误
您需要检查以下内容:
1.循环输入的单元格。
2.对于每个单元格,请查看其本身是否为单元格,如果是,请对单元格的值调用stringFilter
3.如果它不是单元格而是字符数组,则按原样使用其值。
4.否则,如果单元格值包含非字符,则该单元格对输出的贡献为“”(空白)
我认为你犯了一个错误,没有利用in(1)和in{1}之间的差异。
无论如何,这是我的函数版本。它起作用了
function [out] = stringFilter(in)
out = [];
for idx = 1:numel(in)
if iscell (in{idx})
% Contents of the cell is another cell array
tempOut = stringFilter(in{idx});
elseif ischar(in{idx})
% Contents are characters
tempOut = in{idx};
else
% Contents are not characters
tempOut = '';
end
% Concatenate the current output to the overall output
out = [out, tempOut];
end
end
线路
Str = ischar(in{:,:});
这就是问题所在。这对我来说毫无意义
你很快就能得到答案,但犯了一些重大但很小的错误
您需要检查以下内容:
1.循环输入的单元格。
2.对于每个单元格,请查看其本身是否为单元格,如果是,请对单元格的值调用stringFilter
3.如果它不是单元格而是字符数组,则按原样使用其值。
4.否则,如果单元格值包含非字符,则该单元格对输出的贡献为“”(空白)
我认为你犯了一个错误,没有利用in(1)和in{1}之间的差异。
无论如何,这是我的函数版本。它起作用了
function [out] = stringFilter(in)
out = [];
for idx = 1:numel(in)
if iscell (in{idx})
% Contents of the cell is another cell array
tempOut = stringFilter(in{idx});
elseif ischar(in{idx})
% Contents are characters
tempOut = in{idx};
else
% Contents are not characters
tempOut = '';
end
% Concatenate the current output to the overall output
out = [out, tempOut];
end
end
这里有一个不同的含义
function str = stringFilter(in)
if ischar(in)
str = in;
elseif iscell(in) && ~isempty(in)
str = cell2mat(cellfun(@stringFilter, in(:)', 'uni', 0));
else
str = '';
end
end
如果是字符串,则返回它。如果是单元格,则对所有元素应用相同的函数并将它们连接起来。在这里,我使用(:)中的
,
来确保它是一个行向量,然后cell2mat
连接结果字符串。如果类型为其他类型,则返回空字符串。我们需要检查单元格数组是否为空,因为cell2mat({})
的类型是double
这里有一个不同的实现
function str = stringFilter(in)
if ischar(in)
str = in;
elseif iscell(in) && ~isempty(in)
str = cell2mat(cellfun(@stringFilter, in(:)', 'uni', 0));
else
str = '';
end
end
如果是字符串,则返回它。如果是单元格,则对所有元素应用相同的函数并将它们连接起来。在这里,我使用(:)中的
,
来确保它是一个行向量,然后cell2mat
连接结果字符串。如果类型为其他类型,则返回空字符串。我们需要检查单元格数组是否为空,因为cell2mat({})
的类型是double
好的,这实际上是有意义的:)谢谢!好的,这实际上是有道理的:)谢谢!这也是有道理的,谢谢:)这也是有道理的,谢谢:)我明白,但是cell2mat通常都会皱眉,因为我们没有被教过使用它。我明白,但是cell2mat通常会皱眉,因为我们没有被教过使用它