Effacer les filtres
Effacer les filtres

Find duplicate entries and average associated values

60 vues (au cours des 30 derniers jours)
Chris
Chris le 5 Juin 2014
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
  1 commentaire
Cedric
Cedric le 5 Juin 2014
Are elements from the first column positive integers?

Connectez-vous pour commenter.

Réponse acceptée

José-Luis
José-Luis le 5 Juin 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]

Plus de réponses (5)

Cedric
Cedric le 5 Juin 2014
Modifié(e) : Cedric le 5 Juin 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 le 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 le 5 Juin 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 le 5 Juin 2014
Incredible, thanks so much José-Luis and Cedric. I've just learned so many new things from these few lines of code.
  2 commentaires
José-Luis
José-Luis le 5 Juin 2014
No worries.
Please accept an answer if it helped you.
Cedric
Cedric le 5 Juin 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.

Connectez-vous pour commenter.


Kalpana Patel
Kalpana Patel le 19 Avr 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..

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by