【问题标题】:How to restructure histcounts for using with a 2d-matrix如何重组 histcounts 以使用 2d 矩阵
【发布时间】:2015-02-15 06:45:53
【问题描述】:

我在 matlab 中有一个 250000x2 矩阵,其中第一行有一个度数(int,0-360°),第二行有一个与该值对应的浮点值。我的目标是计算每次出现的度值对(例如一行),并将结果写入 nx3 矩阵。 n 在这里对应于唯一行的数量。
因此,我的第一步是获取所有有效的唯一值(使用unique(M, 'rows'))。但现在我想计算所有唯一值。这是通过以下方法完成的:

uniqu_val = unique(values, 'rows');
instance = histcounts(values(:), uniqu_val);

这里我必须输入一个向量作为第二个元素,而不是一个矩阵(uniqu_val 是一个 nx2-dim-matrix)。但是我想获得每个唯一行的出现次数,因此我不能只使用矩阵uniqu_val 的一列。简而言之:我不仅想将 histcounts 用于一维矩阵作为边缘值,而且还用于二维矩阵。我该如何解决这个问题?

【问题讨论】:

    标签: matlab matrix


    【解决方案1】:

    您可以使用unique 的第三个输出,然后像这样使用histcounts -

    %// Find the unique rows and keep the order with 'stable' option
    [uniq_val,~,row_labels] = unique(values, 'rows','stable')
    
    %// Find the counts/instances
    instances = histcounts(row_labels, max(row_labels))
    %// OR with HISTC: instances = histc(row_labels, 1:max(row_labels))
    
    %// Output the unique rows alongwith the counts 
    out = [uniq_val instances(:)]
    

    示例运行 -

    >> values
    values =
         2     1
         3     1
         2     3
         3     3
         1     2
         3     3
         1     3
         3     1
         3     2
         1     2
    >> out
    out =
         2     1     1
         3     1     2
         2     3     1
         3     3     2
         1     2     2
         1     3     1
         3     2     1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-08
      • 2013-09-21
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多