Problem 54730. Local Extrema
Given a 2D (m,n>1) matrix, replace an element if it is not a local extrema (minima/maxima), with 0 (zero).
The comparison criteria for an extrema should be strict (> or <). (See example #1)
Check the 3 neighbours of the corner points, 5 for elements on the edge and 8 for elements in the middle.
%Example #1
x=[1 2 3
2 3 1
3 1 2];
y=[1 0 0
0 0 0
0 0 0];
%Example #2
x=[1 4 7 33 12 6 17
29 8 20 13 3 -1 2
19 14 26 -8 22 20 4
1 6 4 17 34 14 2
-23 27 31 9 30 3 10];
y=[1 0 0 33 0 0 17
29 0 0 0 0 -1 0
0 0 26 -8 0 0 0
0 0 0 0 34 0 2
-23 0 31 0 0 0 0]
%Example #3
x=[-5 -31 -17 -3 -23 -11
-2 -37 -19 -7 -13 -29];
y=[0 0 0 -3 0 -11
-2 -37 0 0 0 -29];
More test cases might be added later.
Solution Stats
Problem Comments
-
6 Comments
Show
3 older comments
Dyuman Joshi
on 11 Jun 2022
William, thanks for the comment.
I thought the value of extrema was based on strict criteria. I stand corrected. The problem statement has been updated.
William
on 12 Jun 2022
Yes, if this were a continuous function, an extremum would imply that the value was strictly the largest. In the case of a set of discrete values, though, it may be debatable whether two equally large neighbors should be considered as 2 maxima or not.
Dyuman Joshi
on 13 Jun 2022
I see, thanks for the clarification!
Solution Comments
Show commentsProblem Recent Solvers6
Suggested Problems
-
5776 Solvers
-
423 Solvers
-
Magic is simple (for beginners)
9730 Solvers
-
83 Solvers
-
334 Solvers
More from this Author31
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!