Doug’s MATLAB Video Tutorials

January 7th, 2009

Binning data in MATLAB

I have been reading the MATLAB questions in Stack Overflow for a few weeks, and here was a really good question that came through. With two vectors, x and y, bin the x values as if with a histogram. Then take the corresponding y values in each bin and take the mean. This uses HISTC and indexing. Mostly this is applying skills from earlier videos.
I really like the Stack Overflow community, and am glad to see the MATLAB questions are being discussed there. Do you have any favorite communities, apart from MATLAB Central, that you like to use? MATLAB Central Files Icon

4 Responses to “Binning data in MATLAB”

  1. Aurélien Queffurust replied on :

    3 years ago we built a community for French MATLAB users here:
    http://www.developpez.net/forums/f148/environnements-developpement/matlab/
    It is like the MATLAB Central except 5 of us can moderate the forum (check spelling, no SMS message, use of HTML markup to insert code or error messages…). It is currently divided in 4 parts : MATLAB , GUI, IMAGE and Signal Processing. For each part we promote the new releases of MATLAB and of course your blogs!

  2. Kenneth Eaton replied on :

    I just checked out Stack Overflow last night, and it looks like a pretty cool community. I think I may start getting active over there.

    Thanks for pointing it out!
    Ken

  3. Ilya Rozenfeld replied on :

    The last loop could’ve been done simpler:

    binMean = accumarray(whichBin, y, [], @mean)

  4. dhull replied on :

    Ilya,

    There are very often ways in MATLAB to accomplish tasks in fewer commands. It is mostly a matter of taste as to which to use.

    I tend to go for things that are easily read and understood by the most people. I needed to look up the doc on ACCUMARRAY to understand the above code. So, I suspect many people, myself included, would take longer to understand the shorter code. It is all trade-offs, and knowing several ways of doing something is great.

    Good find, I will have to add that to my bag of MATLAB tricks.

    -Thanks,
    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.