bin lat lon data and average

5 次查看(过去 30 天)
akk
akk 2019-8-8
Hi, I am trying to bin data by lat and long, and then index the bins. I can get this far:
minlat=min(lat);
minlong=min(long);
gridspacing_lat=6;
gridspacing_lon=5.2
latidx = 1 + floor((lat - minlat) ./ gridspacing_lat);
longidx = 1 + floor((long - minlong) ./ gridspacing_lon);
num=accumarray( [latidx(:), longidx(:)], 1) %finds the # measurements in each grid
This gives me a total of 6 bins with a different number of measurements in each bin. I also have temperature, month and year within this dataset, and I would like to find the monthly mean temperature within each year, and within each bin. I know I can find the means across the whole data set using
G=findgroups(year,month);
temp_mean=splitapply(@nanmean,temp,G)
However, I don't know how to index each bin number. For example, how do I find the monthly mean of each year within bin 1?
Thanks!

回答(1 个)

Kavya Vuriti
Kavya Vuriti 2019-8-14
From question, I understood that there is a dataset containing columns of latitude, longitude, year, month and temperature.
The accumarray function returns a matrix whose dimensions depend on the maximum value of “latidx” and “longidx”. For example, the element num(1,1) gives the number of occurrences of the row [1 1] in the matrix passed as input to accumarray function. As mentioned in the question, there are 6 bins which means there are 6 non-zero elements in “num” array. The indices of these non-zero elements gives the value of latitude and longitude in each bin. Now that we have the values of latitude and longitude of a bin, extract the rows corresponding to that latitude and longitude. Loop through these rows to obtain temperatures of particular month in a year.
For more understanding on accumarray function, refer to the following link:https://www.mathworks.com/matlabcentral/answers/299710-do-not-understand-accumarray-command-in-maltab

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by