Find duplicate entries and average them
9 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Daniel M
on 24 Oct 2019
Edited: Daniel M
on 24 Oct 2019
% 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 Comments
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!