Binning of data except from histcounts?

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)

 Accepted Answer

ndivisions=4;
n = length(A);
partnum = floor(1+(0:n-1)/n*ndivisions);
n1 = accumarray(partnum(:),A(:)==1)

18 Comments

Thank you for making this thing clear to me. I will give a try to your code.
In the case where the array is an exact multiple of ndivisions in length, then
sum(resize(A == 1, [], ndivisions),1)
Bruno is right, I should have written
sum(reshape(A == 1, [], ndivisions),1)
Bruno Luong:You are code working well. how to get the size of the each part here?
"the size of the each part here"
? I'm not sure I understand your wording. Or do you mean how many elements of partnum that has a specific value? In this case
n = accumarray(partnum(:),1)
Here we are diving the total data set in 8 equal parts. How can I calculate the size of the each part ?
Haven't try my answer?
yes I have tried. now i want to get the size of the each part
Just give you, you have not read it or what?
is this n = accumarray(partnum(:),1) ?
n = accumarray(partnum(:),1) this is giving the size of the each part then how can I count no of occurrence of 1's from each part ?
Bruno Luong
Bruno Luong on 27 Oct 2018
Edited: Bruno Luong on 27 Oct 2018
THAT IS ALREADY COMPUTED IN "N1", GRRRR
Is this possible to plot and visualize ??
I have lost track of what would be plotted against what, and what kind of plot would be desired. But probably bar() would be useful.
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 .
Sounds like you would calculate aa vector of the four counts and bar() that .

Sign in to comment.

More Answers (2)

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 Comments

will you please explain this with an example?
[~, idx] = ismember(A, [1 0 nan]);
N = accumarray(idx(:), 1);
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.
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.

Sign in to comment.

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 Comments

I want to binning a dataset. what is this output giving ? and what will be the code for binning a dadaset when there no NaN's in the dataset ?
Bruno Luong
Bruno Luong on 9 Oct 2018
Edited: Bruno Luong on 9 Oct 2018
The output is
  • 0 appears 14 times
  • 1 appears 11 times
  • Nan appears 2 timesin A
Plot the bar with it you'll get your "histogram".
The same code works with or without NaN. I can't see any bins in your question, and with an example with 0,1,NaN hard to see what kind of bins (beside [0,1,NaN]) is meaningful for you.
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 ?
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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!