Starting with an array of x-y-z data, I need to find an array of unique values of x-y and as the z value the max z for the same (x,y) combination.

2 views (last 30 days)
Hi,
I have x,y, z scatter data. I need to find for the same x and y coordinates the maximum z value. so for example:
x y z
1 2 10
2 2 5
2 1 8
2 2 7
I get:
x y z
1 2 10
2 1 8
2 2 7
Order of data is not important. Values in general are not integers.
Thanks for the help, I've tried differents methods with no success.

Accepted Answer

Ameer Hamza
Ameer Hamza on 8 Nov 2020
Edited: Ameer Hamza on 8 Nov 2020
Try this
M = [
1 2 10
2 2 5
2 1 8
2 2 7];
[u_rows, ~, idx] = unique(M(:,[1 2]), 'rows');
max_vals = splitapply(@max, M(:,3), idx);
M_out = [u_rows, max_vals]
Result
>> M_out
M_out =
1 2 10
2 1 8
2 2 7
  1 Comment
Fabrizio Argentieri
Fabrizio Argentieri on 9 Nov 2020
Great! thank you for the quick answer, this worked well, except i had to reduce precision of x,y data otherwhise it missed some nearly identical values.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!