简体   繁体   English

在Matlab中矩阵行上向量化直方图

[英]vectorizing histogram on matrix rows in matlab

Hello I need to calculate a histogram for every row in a big matrix. 您好,我需要为一个大矩阵中的每一行计算一个直方图。 For the first row for example I get this: 例如,对于第一行,我得到以下信息:

AA = hist(symbolic_data(1,:), 1:8);

With symbolic_data(1,:)=[7 6 7 8 7] , I get AA=[0 0 0 0 0 1 3 1] . 使用symbolic_data(1,:)=[7 6 7 8 7] ,我得到AA=[0 0 0 0 0 1 3 1]

Of course this is easy using a simple for loop, but my symbolic_data matrix is really big. 当然,使用简单的for循环很容易,但是我的symbolic_data矩阵确实很大。 Is there a way to vectorize this. 有没有一种向量化的方法。 I've been fiddling with bsxfun , but I can't make it work. 我一直在摆弄bsxfun ,但是我无法使其工作。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks for your time. 谢谢你的时间。

From Matlab help: 从Matlab帮助:

N = hist(Y) bins the elements of Y into 10 equally spaced containers and returns the number of elements in each container. N = hist(Y)将Y的元素分装到10个等距的容器中,并返回每个容器中的元素数。 If Y is a matrix, hist works down the columns. 如果Y是矩阵,则hist在各列下进行运算。

so: 所以:

AA = hist(symbolic_data', 1:8);

will do what you want 会做你想要的

The answer by @Mercury is the way to go. @Mercury答案是正确的方法。 But if you want to do it with bsxfun : 但是,如果要使用bsxfun

  1. If you only have integer values, use 如果只有整数值,请使用

     bin_centers = 1:8; AA = squeeze(sum(bsxfun(@eq, permute(symbolic_data,[2 3 1]), bin_centers(:).'))); 
  2. If the values are not necessarily integer: 如果值不一定是整数:

     bin_centers = 1:8; AA = squeeze(sum( bsxfun(@le, permute(symbolic_data,[2 3 1]), bin_centers(:).'+.5) &... bsxfun(@gt, permute(symbolic_data,[2 3 1]), bin_centers(:).'-.5) )); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM