【问题标题】:Filtering a Cell Array with Recursion使用递归过滤元胞数组
【发布时间】: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


    【解决方案1】:

    你可以将你的函数简化为

    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
    

    【讨论】:

    • 好的,这确实有道理 :) 谢谢!
    【解决方案2】:

    这是一个不同的实现

    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
    

    如果是字符串,则返回。如果它是一个单元格,则对所有元素应用相同的函数并将它们连接起来。这里我使用in(:)' 来确保它是一个行向量,然后cell2mat 连接结果字符串。如果类型是其他类型,则返回一个空字符串。我们需要检查单元格数组是否为空,因为cell2mat({}) 的类型为double

    【讨论】:

    • 我明白这一点,但 cell2mat 通常不受欢迎,因为我们没有被教导使用它
    【解决方案3】:

    线

        Str = ischar(in{:,:});
    

    是问题所在。这对我来说没有任何意义。

    你已经接近得到答案,但犯了一些重大但很小的错误。

    您需要检查以下内容: 1. 循环输入的单元格。 2.对于每一个cell,看它本身是不是一个cell,如果是,就在cell的VALUE上调用stringFilter 3. 如果不是单元格而是字符数组,则直接使用其VALUE。 4.否则如果单元格VALUE包含非字符,则该单元格对输出的贡献为''(空白)

    我认为您没有利用 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
    

    【讨论】:

    • 这也有道理,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多