Given a square matrix A of size n, write a function to create a new matrix B where only the elements on the main diagonal and the anti-diagonal are retained. All other elements should be set to zero.
For instance,
A = [1 2 3;
4 5 6;
7 8 9];
B = [1 0 3;
0 5 0;
7 0 9];

Solution Stats

21 Solutions

17 Solvers

Last Solution submitted on Mar 06, 2026

Last 200 Solutions

Problem Comments

Solution Comments

Show comments
Loading...

Problem Recent Solvers17

Suggested Problems

More from this Author174

Community Treasure Hunt

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

Start Hunting!