Find duplicate entries and average associated values
51 ビュー (過去 30 日間)
古いコメントを表示
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
採用された回答
José-Luis
2014 年 6 月 5 日
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 件のコメント
その他の回答 (5 件)
Cedric
2014 年 6 月 5 日
編集済み: Cedric
2014 年 6 月 5 日
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 件のコメント
derGerät
2015 年 10 月 29 日
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 件のコメント
Chris
2014 年 6 月 5 日
2 件のコメント
Cedric
2014 年 6 月 5 日
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
2021 年 4 月 19 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!