How to insert a smaller diagonal into a Matrix?

2 views (last 30 days)
I'm struggling to generate a matrix M=[0 0 1 0 0; 0 0 0 1 0; 0 0 0 0 1; 0 0 0 0 0; 0 0 0 0 0], quickly without entering each entry by hand. I gather there is a quicker way.
I know I can create the 5x5 identity matrix using M=eye(5) but am unsure how to move the diagonal of 1s across 2. Any help would be appreciated

Accepted Answer

Geoff Hayes
Geoff Hayes on 29 Mar 2018
Monkeyman - try using diag as
D = diag([1 1 1],2)
  1 Comment
Monkeyman
Monkeyman on 29 Mar 2018
I knew it was so simple, I just couldn't think of it, many thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!