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

10 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

  5. Darlene Field replied on :

    Hey Doug

    Thanks so much for the video..made me finally understand how to bin in matlab. One question thought how would you bin x-y-z data?

    Thanks again
    Darlene

  6. dhull replied on :

    @Darlene

    http://blogs.mathworks.com/videos/2007/12/13/visualizing-the-density-of-a-data-cloud/

    Enjoy,
    Doug

  7. Lili replied on :

    I just loved this explanation! Thanks a lot, Doug!

  8. Geoff replied on :

    Exactly the solution I was looking for!

    Many thanks,
    Geoff

  9. irene replied on :

    Hi Doug, thanks for the explanation. I have a question related to your post. After getting the corresponding frequency of occurrence of y in each of the bins on the x-axis, I wanted to plot the data as a histogram plot. I want to come up with a histogram plot that looks like the ones on the following figure. I am not sure how to do it though. Any suggestion will be very helpful.

    http://imageshack.us/photo/my-images/30/sampleplot.gif/

  10. dhull replied on :

    @irene,

    How about a bar plot with bar.m?

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).


MathWorks

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.