MATLAB: How to produce bin averaged (or summed) latitude and longitude grids

grid bin latitude longitude bin averaged grid matrix

I have a set of data with each row representing a series of observations taken at a particular point. Each column represents a different observation as well as information regarding where and when the data was collected (year, month, latitude (column 3), longitude (column 4), rainfall1, error1, rainfall2, and error2). The data needs to be gridded so that the data is over 2.5 degrees latitude 2.5 degrees longitude bin-averaged (or summed up) grids. How would I go about doing this? Here are some additional info from the dataset documentation: latitude (-88.75 –> 88.75), longitude (eastward from 1.25 E).
Thank you.

Best Answer

minlat = -90; %adjust these if needed. They determine where the boxes start
minlong = 0;
gridspacing = 2.5;
latidx = 1 + floor((Latitudes - minlat) ./ gridspacing);
longidx = 1 + floor((Longitudes - minlong) ./ gridspacing);
Now to count the number of entries per grid space:
accumarray( [latidx(:), longidx(:)], 1)
If you want to sum data items within a particular grid cell, replace the "1" with a column vector of the data items.
If you want averaging of the items within a particular grid cell, taking into account only the elements in that grid cell (not dividing by total number of all elements), then
accumarray( [latidx(:), longidx(:)], Observation_column, [], @mean)
Related Question