Binning of data except from histcounts?

2 ビュー (過去 30 日間)
Zara Khan
Zara Khan 2018 年 10 月 9 日
コメント済み: Walter Roberson 2018 年 11 月 14 日
I am a matlab R2014b user. Using histcounts for binning a dataset is not giving me the exact result. Is there any alternative for this ?
A = [0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
N = histcounts(X,6)
>>N =
6 0 2 0 0 2
This what I am getting every time.
A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
C = categorical(A,[1 0 NaN],{'yes','no','undecided'})
[N,Categories] = histcounts(C)
And for the above I am getting this error.
Error using histcounts Expected input number 1, x, to be one of these types:
numeric
Instead its type was categorical.
Error in histcounts (line 96) validateattributes(x,{'numeric'},{'real'}, mfilename, 'x', 1)

採用された回答

Bruno Luong
Bruno Luong 2018 年 10 月 9 日
ndivisions=4;
n = length(A);
partnum = floor(1+(0:n-1)/n*ndivisions);
n1 = accumarray(partnum(:),A(:)==1)
  18 件のコメント
Zara Khan
Zara Khan 2018 年 11 月 14 日
As each array is divided to 4 equal parts then from each part no of occrrence of 1's is being counted. Now I want to plot this total thing .
Walter Roberson
Walter Roberson 2018 年 11 月 14 日
Sounds like you would calculate aa vector of the four counts and bar() that .

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 10 月 9 日
histcounts did not support categorical back then.
Use the second output of ismember to get the bin number, which you can then histc or histcounts or accumarray (most efficient)
  4 件のコメント
Zara Khan
Zara Khan 2018 年 10 月 9 日
A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
>> [~, idx] = ismember(A, [1 0 nan]);
>> N = accumarray(idx(:), 1);
Error using accumarray
First input SUBS must contain positive integer subscripts.
Walter Roberson
Walter Roberson 2018 年 10 月 9 日
Interesting, I did not realize that ismember would not handle nan. It does make a kind of sense, in that nan have the oddity that
nan == nan
is false.

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


Bruno Luong
Bruno Luong 2018 年 10 月 9 日
A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
[U,~,J] = unique(A);
inan = find(isnan(U),1,'first');
if inan
U=U(1:inan);
J=min(J,inan);
end
counts = accumarray(J(:), 1);
[U(:),counts]
ans =
0 14
1 11
NaN 2
  4 件のコメント
Zara Khan
Zara Khan 2018 年 10 月 9 日
Actually I am getting a linear array every and that is not a predefined size. I want to divide that array into 4 or 8 equal parts and wants to count occurence of 1's from each parts . How can I implement this ?
Bruno Luong
Bruno Luong 2018 年 10 月 9 日
Sorry but this is not binning, binning means all the 1s fall at the same place: at the position 1.
If you use the wrong wording you should expect get the wrong answer.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by