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
Solution Comments
Show comments
Loading...
Problem Recent Solvers9
Suggested Problems
-
Replace NaNs with the number that appears to its left in the row.
3071 Solvers
-
It dseon't mettar waht oedrr the lrettes in a wrod are.
2170 Solvers
-
Project Euler: Problem 6, Natural numbers, squares and sums.
2583 Solvers
-
360 Solvers
-
Calculate compression ratio of engine
235 Solvers
More from this Author44
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Dyuman, Test 5 is missing a parenthesis.
Fixed. It's always something with my test suite :')
Dyuman, Thanks for the problem. It might be good to add that what you mean by a local maximum is not only that the value is the largest value, but also that it is strictly greater than the second largest. Likewise for a local minimum.
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.
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.
I see, thanks for the clarification!