【问题标题】:Matlab: find mode in rangeMatlab:在范围内查找模式
【发布时间】:2012-09-07 07:52:33
【问题描述】:

我有一个像这样的矩阵:

A=
    10 31 32 22
    32 35 52 77
    68 42 84 32

我需要一个像mode 这样的函数,但具有范围,例如返回 30 的 mymode(A,10),在 0-10、10-20、20-30 等范围内找到最常见的数字。返回范围内的大多数数字。

【问题讨论】:

    标签: matlab mode


    【解决方案1】:

    我猜根据不同答案的数量,我们可能会错过您的目标。这是我的解释。

    如果您想要有多个范围并且希望为每个范围输出最频繁的数字,请创建一个包含所有所需范围的单元格(它们可能重叠)并使用 cellfun 为每个范围运行 mode() 。您还可以使用 arrayfun 以类似的方式创建具有所需范围的单元格:

    A = [10 31 32 22; 32 35 52 77; 68 42 84 32];
    
    % create ranges
    range_step = 10;
    range_start=[0:range_step:40];
    range=arrayfun(@(r)([r r+range_step]), range_start, 'UniformOutput', false)
    
    % analyze ranges
    o = cellfun(@(r)(mode(A(A>=r(1) & A<=r(2)))), range, 'UniformOutput', false)
    
    o = 
    [10]    [10]    [22]    [32]    [42]
    

    【讨论】:

      【解决方案2】:

      您可以使用histc 将您的数据分箱到您想要的范围内,然后在histc 的输出中找到使用max 的成员最多的分箱

      ranges = 0:10:50;                 % your desired ranges
      [n, bins] = histc(A(:), ranges);  % bin the data
      [v,i] = max(n);                   % find the bin with most occurrences
      
      [ranges(i) ranges(i+1)]           % edges of the most frequent bin
      

      对于您的具体示例,这将返回

      ans =
      
          30    40
      

      与您所需的输出相匹配,因为 A 中的大多数值介于 30 到 40 之间。

      【讨论】:

        【解决方案3】:
        > A = [10 31 32 22; 32 35 52 77; 68 42 84 32]
        A =
        
           10   31   32   22
           32   35   52   77
           68   42   84   32
        
        > min = 10
        min =  10
        > max = 40
        max =  40
        > mode(A(A >= min & A <= max))
        ans =  32
        > 
        

        【讨论】:

          【解决方案4】:
          [M,F] = mode(  A((A>=2) & (A<=5))  ) %//only interested in range 2 to 5
          

          ...其中 M 将为您提供模式,F 将为您提供出现频率

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-22
            • 2016-10-08
            • 1970-01-01
            相关资源
            最近更新 更多