Create a matrix with a diagonal and left-diagonal of all 1s in MATLAB

Viewed 632

I would like to create a square matrix of size n x n where the diagonal elements as well as the left-diagonal are all equal to 1. The rest of the elements are equal to 0.

For example, this would be the expected result if the matrix was 5 x 5:

1 0 0 0 0
1 1 0 0 0
0 1 1 0 0
0 0 1 1 0
0 0 0 1 1

How could I do this in MATLAB?

5 Answers
Related