3
votes

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
Use diag twice, add them together.excaza
Always only two diagonals of 1s?Dan
@Dan yes allways with onesefirvida
@efirvida I have changed the wording of your question as well as the title to better reflect what is being asked. A title such as "Matlab how to create this matrix" is not a meaningful title. If there are other people who have the same problem as you, making the title more meaningful will go a long way to allow the question to be easily searchable. Let me know what you think, or if you want me to revert the edits.rayryeng
@rayryeng thanks for allefirvida

5 Answers

6
votes

Trivial using the tril function:

tril(ones(n),0) - tril(ones(n),-2)

And if you wanted a thicker line of 1s just adjust that -2:

n = 10;
m = 4;
tril(ones(n),0) - tril(ones(n),-m)

If you prefer to use diag like excaza suggested then try

diag(ones(n,1)) + diag(ones(n-1,1),-1)

but you can't control the 'thickness' of the stripe this way. However, for a thickness of 2, it might perform better. You'd have to test it though.

5
votes

You can also use spdiags too to create that matrix:

n = 5;
v = ones(n,1);
d = full(spdiags([v v], [-1 0], n, n));

We get:

>> d

d =

     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

The first two lines define the desired size of the matrix, assuming a square n x n as well as a vector of all ones that is of length n x 1. We then call spdiags to define where along the diagonal of this matrix this vector will be populating. We want to define the main diagonal to have all ones as well as the diagonal to the left of the main diagonal, or -1 away from the main diagonal. spdiags will adjust the total number of elements for the diagonal away from the main to compensate.

We also ensure that the output is of size n x n, but this matrix is actually sparse . We need to convert the matrix to full to complete the result.,

5
votes

With a bit of indices juggling, you can also do this:

N = 5;
ind = repelem(1:N, 2);    % [1 1 2 2 3 3 ... N N]
M = full(sparse(ind(2:end), ind(1:end-1), 1))
5
votes

Simple approach using linear indexing:

n = 5;
M = eye(n);
M(2:n+1:end) = 1;
3
votes

This can also be done with bsxfun:

n = 5;        %// matrix size
d = [0 -1];   %// diagonals you want set to 1
M = double(ismember(bsxfun(@minus, 1:n, (1:n).'), d));

For example, to obtain a 5x5 matrix with the main diagonal and the two diagonals below set to 1, define n=5 and d = [0 -1 -2], which gives

M =
     1     0     0     0     0
     1     1     0     0     0
     1     1     1     0     0
     0     1     1     1     0
     0     0     1     1     1