Find duplicate entries and average associated values
51 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Risposta accettata
José-Luis
il 5 Giu 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]
0 Commenti
Più risposte (5)
Cedric
il 5 Giu 2014
Modificato: Cedric
il 5 Giu 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
0 Commenti
derGerät
il 29 Ott 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...
0 Commenti
Chris
il 5 Giu 2014
2 Commenti
Cedric
il 5 Giu 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.
Kalpana Patel
il 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..
0 Commenti
Vedere anche
Categorie
Scopri di più su Data Distribution Plots in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!