# Binning data in MATLAB 23

Posted by **Doug Hull**,

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,
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?

**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.## 23 CommentsOldest to Newest

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! : **1**of 23
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 : **2**of 23
Ilya Rozenfeld
replied on

The last loop could've been done simpler:
binMean = accumarray(whichBin, y, [], @mean) : **3**of 23
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 : **4**of 23
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 : **5**of 23
dhull
replied on

@Darlene
https://blogs.mathworks.com/videos/2007/12/13/visualizing-the-density-of-a-data-cloud/
Enjoy,
Doug : **6**of 23
Lili
replied on

I just loved this explanation! Thanks a lot, Doug! : **7**of 23
Geoff
replied on

Exactly the solution I was looking for!
Many thanks,
Geoff : **8**of 23
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/ : **9**of 23
dhull
replied on

@irene,
How about a bar plot with bar.m? : **10**of 23
Megan
replied on

Hi Doug,
Thanks for posting this! I am trying to solve the same problem, but with three dimensional data. I have points in x,y coordinate space and right now I am plotting a 2D histogram with color label corresponding to the number of points in each bin. Instead, I would like to have color correspond to the mean of a third vector z. This should be simple given that you've solved the main problem, but I am getting confused with the indexing once the extra dimension is added. Any help would be much appreciated!
thanks again,
megan : **11**of 23
prasad
replied on

I tried to use this method and I end up getting following error:
Error using .*
Matrix dimensions must agree.
Error in linspace (line 31)
y = d1 + (0:n1).*(d2 - d1)/n1;
So I debug into the linsapce code and looks like if you have negative values it breaks. And I have negative values in my continues matrix. Any solution? : **12**of 23
Doug
replied on

You need to say what the values of those variables are for scalars and size of the vectors before we can help much here. : **13**of 23
Julie
replied on

This code is very helpful. Thank you.
Now that I know how to bin data for one data set (x and y), I would like to bin multiple data sets together. I have three data sets that are time series at three different locations. For all the data sets x is years and y is temperature. The time steps are not evenly spaced between the different data sets. Is there a way to bin the data from all the data sets that was taken in a given time step. For example, can I find the mean of all three data sets taken between 1950-2000.
Is the best way to bin each data set first and then just find the mean during that time step or is there a shorter way?
Thank you. : **14**of 23
Doug
replied on

@Julie,
please see support@mathworks.com with more details. It is not clear what you need and that is a better venue to get help. : **15**of 23
Emily
replied on

This is great. Would it accomplish this task? I have x= aspect(1:360)degrees, and y=slope values. I want to see if certain slopes are correlated to certain aspects by binning them in 20 degree increments. So if my edges were 0,360 and numbing =18 then this would work? : **16**of 23
Zach
replied on

Hi Doug, this is very helpful. I'm trying to do essentially the same thing you've done here, but find the weighted mean of all the values in the bin (each value has an associated uncertainty: e.g. 13.5 +/-0.3). The weighted mean is calculated by first finding the inverse of the sum of the inverse square of all the errors, then multiplying that by the sum of the value divided by the square of its error. This means that number of elements in each bin depends on the number of values in the bin. Do you have any suggestions for how to code the weighted mean calculation in the loop?
Many thanks! : **17**of 23
Doug
replied on

@Zach,
It sounds like you know the math. What part of this is giving you problems? Feel free to e-mail me directly with the code you have written and show me what is going wrong for you.
Doug : **18**of 23
Nafi3
replied on

Thanks for this brilliant post.
Now I'm trying to perform it for three vectors, x, y,and z. It means bin x and y values and then calculate the mean of the corresponding z for both x and y. I am wondering it is possible or not? : **19**of 23
Doug
replied on

@Nafi,
It is not clear what you are proposing, but sounds doable. : **20**of 23
ye Song
replied on

hello Doug,I'm ye Song,and I'm from China.Now i'm doing the task of joint pdf(aim at joint entropy).I wonder if i could use your code to calculate the x*y, 2 dimension.p(x,y).Or I should use the function accumarray.THANK YOU !Look forward to your early reply！ : **21**of 23
Stuart McGarrity
replied on
:

Hi ye Song, I'm not sure how the joint PDF is calculated.It may be more understandable to implement first as a for loop, make sure you get the right answer and then see if accumarray is appropriate. **22**of 23
Peter Swords
replied on

I realise the post is more than nine(!) years old now, but really clear and useful and just what I needed as a noob with Octave/MatLab. Thanks Doug. : **23**of 23
## Recent Comments