Sort items to recycle bins in MATLAB

If I have a dataset Y and a set of bins centered in X, I can use the HIST command to determine how many of each Y are in each drawer.

N = hist(Y,X) 

What I would like to know is a built-in function that can tell me which box each Y goes in, so

 [N,I] = histMod(Y,X) 

means that Y (I == 1) will return all Y in cell 1, etc.

I know how to write this function, so I only wonder if there is a built-in module in MATLAB that does this.

+4
source share
2 answers

The associated histc function does this, but this requires defining the edges of the bin instead of the bin centers.

 Y = rand(1, 10); edges = .1:.1:1; [N, I] = histc(Y, edges); 

Calculation of edges, given that binzenters are also easy. In one insert:

 N = hist(Y, X); 

becomes

 [Nc, Ic] = histc(Y, [-inf X(1:end-1) + diff(X)/2, inf]); 

with Nc == N, plus one extra blank bit at the end (since I don't assume that the value in Y matches inf). See doc histc .

+6
source

If it’s appropriate to use bin edges instead of boxes,

 [N,bin] = histc(y,binedges) 

works. Aaargh, MATLAB, your function definitions are so unintuitive

+2
source

All Articles