views:

2115

answers:

2
+3  Q: 

Binning in matlab

I have been unable to find a function in matlab or octave to do what I want. I have a matrix m of two columns (x and y values). I know that I can extract the column by doing m(:,1) or m(:,2). I want to split it into smaller matricies of [potentially] equal size and and plot the mean of these matricies. In other words, I want to put the values into bins based on the x values, then find means of the bins. I feel like the hist function should help me, but it doesn't seem to.

Does anyone know of a built-in function to do something like this?

edit

I had intended to mention that I looked at hist and couldn't get it to do what I wanted, but it must have slipped my mind.

Example: Let's say I have the following (I'm trying this in octave, but afaik it works in matlab):

x=1:20;
y=[1:10,10:1];
m=[x, y];

If I want 10 bins, I would like m to be split into:

m1=[1:2, 1:2]
...
m5=[9:10, 9:10]
m6=[10:11, 10:-1:9]
...
m10=[19:20, 2:-1:1]

and then get the mean of each bin.

Update: I have posted a follow-up question here. I would greatly appreciate responses.

A: 
>help hist


HIST  Histogram.
N = HIST(Y) bins the elements of Y into 10 equally spaced containers
and returns the number of elements in each container.  If Y is a
matrix, HIST works down the columns.

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. The first bin includes
data between -inf and the first center and the last bin
includes data between the last bin and inf. Note: Use HISTC if
it is more natural to specify bin edges instead. 

[N,X] = HIST(...) also returns the position of the bin centers in X.

HIST(...) without output arguments produces a histogram bar plot of
the results. The bar edges on the first and last bins may extend to
cover the min and max of the data unless a matrix of data is supplied.

HIST(AX,...) plots into AX instead of GCA.

Class support for inputs Y, X: 
   float: double, single

See also histc, mode.

Overloaded methods:
   fints/hist

Reference page in Help browser
   doc hist
Marc
Just giving me the doc string of a function is a little insulting.
Alex R
I'm truly sorry - I just thought that it was as succinct and accurate as possible. I thought just putting "look at hist" was a bit too terse. No insult intended - I know that learning the "library" in any new language is the most daunting task.
Marc
+6  A: 

I have answered this in video form on my blog:

http://blogs.mathworks.com/videos/2009/01/07/binning-data-in-matlab/

Here is the code:

m = rand(10,2); %Generate data

x = m(:,1); %split into x and y
y = m(:,2);

topEdge = 1; % define limits
botEdge = 0; % define limits
numBins = 2; % define number of bins

binEdges = linspace(botEdge, topEdge, numBins+1);

[h,whichBin] = histc(x, binEdges);

for i = 1:numBins
    flagBinMembers = (whichBin == i);
    binMembers     = y(flagBinMembers);
    binMean(i)     = mean(binMembers);
end
MatlabDoug
Very clear and simple. Thank you.
Alex R

related questions