Find duplicate entries and average them

15 Ansichten (letzte 30 Tage)
Vivek Gupta
Vivek Gupta am 24 Okt. 2019
Bearbeitet: Daniel M am 24 Okt. 2019
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?

Akzeptierte Antwort

Daniel M
Daniel M am 24 Okt. 2019
Bearbeitet: Daniel M am 24 Okt. 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');

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by