Find duplicate entries and average associated values

111 views (last 30 days)
Hi all,
I have a 180 x 2 array of data and in the first column there are some repeated values (multiple data points at the same location). With those repeated values I'd like to average the associated data in column 2. Any help with this would be appreciated greatly.
Example array: [1 10; 2 20; 3 30; 4 40; 4 50; 4 60; 5 70; 6 80; 7 90; 7 100; 8 110]
I'd like to create a new array that looks like this: [1 10; 2 20; 3 30; 4 50; 5 70; 6 80; 7 95; 7 100; 8 110]
Thanks a ton for any help.
-Chris

Accepted Answer

José-Luis
José-Luis on 5 Jun 2014
array = [1 10; 2 20; 3 30; 4 40; 4 50; 4 60; 5 70; 6 80; 7 90; 7 100; 8 110];
[C,ia,idx] = unique(array(:,1),'stable');
val = accumarray(idx,array(:,2),[],@mean);
your_mat = [C val]

More Answers (5)

Cedric
Cedric on 5 Jun 2014
Edited: Cedric on 5 Jun 2014
If elements from the first column are "continuous" positive integers, here is a way to go:
>> data = [1 10; 2 20; 3 30; 4 40; 4 50; 4 60; 5 70; 6 80; 7 90; 7 100; 8 110] ;
>> newData = [unique( data(:,1) ), ...
accumarray( data(:,1), data(:,2), [], @mean )]
newData =
1 10
2 20
3 30
4 50
5 70
6 80
7 95
8 110
If you don't have "continuity" (i.e. there are gaps), here is a more general solution (I removed row 5 70 from previous data):
>> data = [1 10; 2 20; 3 30; 4 40; 4 50; 4 60; 6 80; 7 90; 7 100; 8 110] ;
>> uc1 = unique( data(:,1) ) ;
>> mc2 = accumarray( data(:,1), data(:,2), [], @mean ) ;
>> newData = [uc1, mc2(uc1)]
newData =
1 10
2 20
3 30
4 50
6 80
7 95
8 110

derGerät
derGerät on 29 Oct 2015
Hey all, I have kind of the same problem but my data ar not in one array but two arrays. Is it possible to adept this solution to make it work with two arrays?
Sorry if thats a stupid question but I'm a beginner...

Chris
Chris on 5 Jun 2014
This is great, but the first column elements are actually not integers. They are continuous, but they have precision up to three decimal points.
Thanks for the quick reply.

Chris
Chris on 5 Jun 2014
Incredible, thanks so much José-Luis and Cedric. I've just learned so many new things from these few lines of code.
  2 Comments
José-Luis
José-Luis on 5 Jun 2014
No worries.
Please accept an answer if it helped you.
Cedric
Cedric on 5 Jun 2014
My pleasure, note that I assumed integers because, as far as I am concerned, it is rarely appropriate to use UNIQUE on floating points, for the same reason it is rarely appropriate to test equality between floats.

Sign in to comment.


Kalpana Patel
Kalpana Patel on 19 Apr 2021
how to filter the repeated datain a data series maintaining the length. I have two column, one is for latitude and second is for ion density, with the use of unique code length is maot maintain. reason i know. so just want single latitude valua and data corresponding to it. total data values are 4892. pls help..

Products

Community Treasure Hunt

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

Start Hunting!