22 CommentsOldest to Newest
3 years ago we built a community for French MATLAB users here:
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!
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!
The last loop could’ve been done simpler:
binMean = accumarray(whichBin, y, , @mean)
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 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?
I just loved this explanation! Thanks a lot, Doug!
Exactly the solution I was looking for!
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.
How about a bar plot with bar.m?
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!
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?
You need to say what the values of those variables are for scalars and size of the vectors before we can help much here.
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?
please see firstname.lastname@example.org with more details. It is not clear what you need and that is a better venue to get help.
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?
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?
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.
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?
It is not clear what you are proposing, but sounds doable.
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！
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.