Efficiently find duplicate values and then average other value in rows and remove extra rows?

10 views (last 30 days)
I have data that sometimes has more than one measurement for a single date. I would like to interpolate the data, so I need to create single values for each date. I would like to do this by finding duplicate dates, averaging the data value for the date, and then replacing the original rows with the new single row. How to do this?
For example (made up data):
Starting data:
[2009.02, 120;
2009.14, 150;
2009.14, 190;
2009.24, 70;
2009.34, 80;
2009.52, 90;
2009.52, 150;
2009.52, 60]
Would become:
[2009.02, 120;
2009.14, 170;
2009.24, 70;
2009.34, 80;
2009.52, 100]
Thank you!

Accepted Answer

the cyclist
the cyclist on 29 Aug 2013
Edited: the cyclist on 29 Aug 2013
Tailor-made problem for the accumarray() function:
A = [2009.02, 120;
2009.14, 150;
2009.14, 190;
2009.24, 70;
2009.34, 80;
2009.52, 90;
2009.52, 150;
2009.52, 60];
[UA,~,idx] = unique(A(:,1));
NEW_A = [UA,accumarray(idx,A(:,2),[],@mean)]

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!