필터 지우기
필터 지우기

Find duplicate entries and average them

조회 수: 9 (최근 30일)
Vivek Gupta
Vivek Gupta 2019년 10월 24일
편집: Daniel M 2019년 10월 24일
Hello,
I am trying to do something similar to this post, but with three columns instead of two. I start with three vectors of the same size. x and y will have values rounded to the nearest 10, but they are not neccessarily in asscending or descending order, and some numbers can be skipped.
x = [0, 10, 20, 20, 20, 30, 40, 50, 50, 70];
y = [0, 10, 10, 20, 20, 30, 40, 50, 60, 60];
z = [10, 20, 40, 30, 5, 2, 11, 19, 19, 14];
If there are any (x,y) pairs that are duplicates, I want to remove the duplicate x and y values and average their corresponding z values. So the result should look like:
x = [0, 10, 20, 20, 30, 40, 50, 50, 70];
y = [0, 10, 10, 20, 30, 40, 50, 60, 60];
z = [10, 20, 40, 17.5, 2, 11, 19, 19, 14];
Any ideas how I could do this efficienctly without using find and a for loop?

채택된 답변

Daniel M
Daniel M 2019년 10월 24일
편집: Daniel M 2019년 10월 24일
% original data
x = [0, 10, 20, 20, 20, 30, 40, 50, 50, 70];
y = [0, 10, 10, 20, 20, 30, 40, 50, 60, 60];
z = [10, 20, 40, 30, 5, 2, 11, 19, 19, 14];
% remove duplicate pairs of (x,y), and find those locations
[newXY,~,locMembers] = unique([x',y'],'rows','stable');
xx = newXY(:,1)';
yy = newXY(:,2)';
% group the values of z by the duplicate pairs, take the mean
zz = splitapply(@(v) mean(v), z, locMembers');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by