Find duplicate entries and average them
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Vivek Gupta
el 24 de Oct. de 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?
0 comentarios
Respuesta aceptada
Daniel M
el 24 de Oct. de 2019
Editada: Daniel M
el 24 de Oct. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!