I have a cell array in which some of the entries have two data points. I want to average the two data points if the data were collected on the same day.
The first column of cell array 'site' is the date. The fourth column is the data concentration. I want to average the fourth column if the data comes from the same day.
For example, if my cell array looks like this:
01/01/2011 36-061-0069 1 10.4
01/01/2011 36-061-0069 2 10.1
01/04/2011 36-061-0069 1 7.9
01/05/2011 36-061-0069 1 13
I want to average the fourth column (10.4 and 10.1) into one row and leave everything else the same.
Help? Would an if elseif loop work? I'm not sure how to approach this issue, especially since cell arrays work a little differently than matrices.
It sounds like you want to do :
You can do it succinctly without a loop, using a combination of
unique
,diff
andaccumarray
.Define data:
Then:
gives the desired result:
If needed, you can get the non-repeated, sorted dates with
data(ind_sort(ii),1)
.Explanation of the code: the dates are first converted to numbers and sorted. The unique dates and repeated dates are then extracted. Finally, data in repeated rows are summed and divided by the number of repetitions to obtain the averages.
Compatibility issues for Matlab 2013a onwards:
The function
unique
has changed in Matlab 2013a. For that version onwards, add'legacy'
flag tounique
, i.e. replace the line[~, ii, jj] = unique(dates_sort)
by