Given an array of co-ordinates, for each value of x, how do I extract the smallest value of y?

1 view (last 30 days)
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!

Accepted Answer

Ameer Hamza
Ameer Hamza on 6 Nov 2020
Edited: Ameer Hamza on 6 Nov 2020
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)]

More Answers (2)

KSSV
KSSV on 6 Nov 2020
LEt (x,y) be your points.
[c,ia,ib] = unique(x) ;
n = length(c) ;
iwant = zeros(n,2) ;
for i = 1:n
iwant(i,1) = c(i) ;
iwant(i,2) = min(y(ib==i)) ;
end
iwant

madhan ravi
madhan ravi on 6 Nov 2020
Edited: madhan ravi on 6 Nov 2020
x = [1; 1; 1; 2; 2; 3; 3; 3];
y = [1; 2; 3; 3; 2; 1; 2; 3];
T = array2table([x, y], 'VariableNames', {'x', 'y'});
T1 = groupsummary(T, 'x', 'min')
T1 = 3x3 table
x GroupCount min_y _ __________ _____ 1 3 1 2 2 2 3 3 1
Wanted = T1{:,{'x', 'min_y'}}
Wanted = 3×2
1 1 2 2 3 1

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!