필터 지우기
필터 지우기

Find duplicate entries and average associated values

조회 수: 93 (최근 30일)
Chris
Chris 2014년 6월 5일
답변: Kalpana Patel 2021년 4월 19일
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
Cedric
Cedric 2014년 6월 5일
Are elements from the first column positive integers?

댓글을 달려면 로그인하십시오.

채택된 답변

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

추가 답변 (5개)

Cedric
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

derGerät
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...

Chris
Chris 2014년 6월 5일
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 2014년 6월 5일
Incredible, thanks so much José-Luis and Cedric. I've just learned so many new things from these few lines of code.
  댓글 수: 2
José-Luis
José-Luis 2014년 6월 5일
No worries.
Please accept an answer if it helped you.
Cedric
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
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..

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by