Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 268
Alex R Avatar asked Jan 07 '09 04:01

Alex R


1 Answers

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
like image 107
MatlabDoug Avatar answered Sep 23 '22 18:09

MatlabDoug