Problem 60930. 'X'avier

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

93.33% Correct | 6.67% Incorrect
Last Solution submitted on Jul 04, 2025

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers12

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!