Finding mean of data between rows data based on information in column data

I have a matrix in which the rows represent a sample and the columns represent the data for each of the samples. One of the columns (6) contains a large amount of latitudes. Some of these latitude overlap and some are very similar. As there is an uneven spread of latitudes I wish to get the mean and standard deviation of the mean for the data in column 13 for the different samples that fall between half a degree in latitude (ie so that the data of any samples latitude that falls between -60 and -60.499 is put together to derive a mean and standard deviation from this). How would I go about doing this? Thank you, Natalie

 Accepted Answer

Doesn't my answer to your previous question about binning answer this question as well?
If you didn't want to bin the data set like in your previous question, but compute some statistics for entries whose latitude fall in a given range, e.g. ]-60.5,-60], you could do it as follows:
id = data(:,6) > -60.5 & data(:,6) <= -60 ;
selection = data(id,13) ;
theMean = mean(selection) ;
theStd = std(selection) ;
etc ..
If you wanted to bin by latitude, i.e. obtain a statistics on bands, you could proceed the same way we did in your previous question, but this time on a "1D array". Assuming that you data lies in the range of latitudes [-89.5, 89.5]
lat_binID = 181 + ceil( data(:,6) ./ 0.5 ) ;
mean_bin = accumarray( lat_binID, data(:,13), [361, 1], @mean ) ;
std_bin = accumarray( lat_binID, data(:,13), [361, 1], @std) ;
Note that I didn't test this code, so you would have to check that it is working.

3 Comments

both work, however the first isn't feasible as there are many latitudes. The outcome isn't entirely as what was wanted but have somewhat altered it accordingly (ie. altered it to produce latitudes between desired range of latitudes). My only problem now is I want to plot the mean plus or minus the standard deviation against the latitude. I have plotted the mean against the latitude but am unsure as to how to add in the standard deviation from the mean. Do you know how this could be done? Thank you once again
You're welcome. The whole approach should be something like
lat_binID = 181 + ceil( data(:,6) ./ 0.5 ) ;
mean_bin = accumarray( lat_binID, data(:,13), [361, 1], @mean ) ;
std_bin = accumarray( lat_binID, data(:,13), [361, 1], @std) ;
And then, if the purpose is to plot only non-zero bin statistics..
lat_bin = -90:0.5:90 ;
id_nz = mean_bin ~= 0 ;
figure(1) ; clf ; hold on ;
plot( lat_bin(id_nz), mean_bin(id_nz) ) ;
errorbar( lat_bin(id_nz), mean_bin(id_nz), ...
-std_bin(id_nz), std_bin(id_nz) ) ;
Note that you might have enough data points in each bin not to have to eliminate "empty bins" from display.
awesome, thank you so much!! worked perfectly

Sign in to comment.

More Answers (0)

Categories

Asked:

on 31 Oct 2013

Commented:

on 6 Nov 2013

Community Treasure Hunt

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

Start Hunting!