Find duplicate entries and average them

1 次查看(过去 30 天)
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