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

14 Solutions

9 Solvers

Last Solution submitted on Aug 13, 2026

Last 200 Solutions

Problem Comments

Solution Comments

Show comments
Loading...

Problem Recent Solvers9

Suggested Problems

More from this Author44

Community Treasure Hunt

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

Start Hunting!