Loop-free histogramming of each column of an input matrix

1 回表示 (過去 30 日間)
Matt J
Matt J 2022 年 3 月 5 日
コメント済み: Matt J 2022 年 3 月 6 日
Before there was histcounts(), we would use histc(), which is no longer recommended by TMW. But histc() could calculate the column-wise histogram of an input matrix,
A =[ 9 5 2 1 5 3 3 2 2 5
1 8 3 1 7 4 3 2 2 6
1 3 1 1 8 6 4 4 2 4
1 3 1 6 4 2 1 2 9 7
2 1 6 3 2 2 1 1 9 1
2 3 1 1 7 5 4 2 1 5
1 1 4 2 1 8 6 5 4 5];
H=histc(A,1:10)
H = 10×10
4 2 3 4 1 0 2 1 1 1 2 0 1 1 1 2 0 4 3 0 0 3 1 1 0 1 2 0 0 0 0 0 1 0 1 1 2 1 1 1 0 1 0 0 1 1 0 1 0 3 0 0 1 1 0 1 1 0 0 1 0 0 0 0 2 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
whereas histcounts does not seem to have this capability. Is there a replacement for this capability somewhere in modern Matlab, aside from an Mcoded for-loop?

採用された回答

Dave B
Dave B 2022 年 3 月 6 日
This is somewhere between workaround and hack, but you could do it with histcounts2:
X=rand(5);
a=histcounts2(X,ones(5).*(1:5),linspace(0,1,10),.5:5.5);
b=histc(X,linspace(0,1,10));
isequal(a,b(1:end-1,:))
ans = logical
1
  1 件のコメント
Matt J
Matt J 2022 年 3 月 6 日
Yep. That works. Too bad about the extra memory allocation required with ones(N).*(1:N)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by