【问题标题】:2d matrix histogram in matlab that interprets each column as a separate elementmatlab中的二维矩阵直方图,将每一列解释为一个单独的元素
【发布时间】:2012-06-21 14:20:51
【问题描述】:

我在 matlab 中有一个 128 x 100 矩阵,其中每一列都应被视为一个单独的元素。我们称这个矩阵为 M。

我还有另一个 128 x 2000 矩阵(称为 V),由矩阵 M 的列组成。

如何制作一个直方图来映射第二个矩阵中使用的每一列的频率?

hist(double(V),double(M)) gives the error:

 Error using histc
Edge vector must be monotonically
non-decreasing.

我该怎么办?

【问题讨论】:

    标签: matlab histogram


    【解决方案1】:

    这是一个例子。我们从与您描述的数据相似的数据开始

    %# a matrix of 100 columns
    M = rand(128,100);
    sz = size(M);
    
    %# a matrix composed of randomly selected columns of M (with replacement)
    V = M(:,randi([1 sz(2)],[1 2000]));
    

    然后:

    %# map the columns to indices starting at 1
    [~,~,idx] = unique([M,V]', 'rows', 'stable');
    idx = idx(sz(2)+1:end);
    
    %# count how many times each column occurs
    count = histc(idx, 1:sz(2));
    
    %# plot histogram
    bar(1:sz(2), count, 'histc')
    xlabel('column index'), ylabel('frequency')
    set(gca, 'XLim',[1 sz(2)])
    

    【讨论】:

      【解决方案2】:

      [Lia,Locb] = ismember(A,B,'rows') 还返回一个向量 Locb, 对于 A 中的每一行(也是一行),包含 B 中的最高索引 在 B 中。输出向量 Locb 包含 0,只要 A 不是 B.

      ismemberrows 参数可以识别另一个矩阵的行来自一个矩阵的哪一行。由于它适用于行,并且您正在寻找列,因此只需转置两个矩阵。

      [~,Locb]=ismember(V',M');
      histc(Locb)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多