簡體   English   中英

在Matlab中使用hist來計算出現次數

[英]Using hist in Matlab to compute occurrences

我使用hist來計算Matlab中矩陣中值的出現次數。

我認為我使用它是錯誤的,因為它給了我完全奇怪的結果。 你能幫我理解發生了什么嗎?

當我運行這段代碼時,我會根據需要獲得countsB

rng default; 
B=randi([0,3],10,1);
idxB=unique(B);
countsB=(hist(B,idxB))';

B=[3;3;0;3;2;0;1;2;3;3];
idxB=[0;1;2;3];
countsB=[2;1;2;5];

當我運行另一段代碼時,我得到了countsA錯誤結果

A=ones(524288,1)*3418;
idxA=unique(A);
countsA=(hist(A,idxA))';

idxA=3148;
countsA=[zeros(1709,1); 524288; zeros(1708,1)];

我究竟做錯了什么?

idxA是一個標量,表示此上下文中的bin數。 將idxA設置為向量而不是例如[0,3418]將獲得一個以0和3418為中心的bin的hist,類似於idxB所獲得的,也是一個向量

要添加到其他答案:您可以使用顯式總和替換hist

idxA = unique(A);
countsA = sum(bsxfun(@eq, A(:), idxA(:).'), 1);

我認為這與:

N = HIST(Y,M), where M is a scalar, uses M bins.

我認為你認為它會這樣做:

N = HIST(Y,X), where X is a vector, returns the distribution of Y
among bins with centers specified by X.

換句話說,在第一種情況下,matlab假設您要求3418個箱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM