MATLAB: Given an array of co-ordinates, for each value of x, how to extract the smallest value of y

arrayarraysimage analysisimage processingImage Processing Toolboximage segmentationindexing

Context:
I have the pixel co-ordinates of the white area of a binary image.
For each x co-ordinate (1:1024) I want to find the smallest value of y. I want to do this because I want to find the 1D upper line boundary co-ordinates of a white splodge that bisects the image (see attached).
e.g for
x y
1 1
1 2
1 3
2 3
2 2
3 1
3 2
3 3
I want to be able to extract (1,1), (2,2), and (3,1) automatically, into a new array.
Thanks!

Best Answer

Try something like this
M = [
1 1
1 2
1 3
2 3
2 2
3 1
3 2
3 3];
x = M(:,1);
y = M(:,2);
out = [(1:max(x)).' splitapply(@min, y, x)]
Result
>> out
out =
1 1
2 2
3 1
An alternative solution using accumarray
out = [(1:max(x)).' accumarray(x, y, [], @min)]