MATLAB: How to extract a number of diagonals of a matrix

diagonals

Hello all,
I have an N-by-N matrix. I want to extract the main diagonal and d diagonals to its right and d diagonals to its left and null all other elements. How can I do that?
Thanks

Best Answer

The diag function can do everything you want. You just have to ask it!