# How to create a diagonal matrix that starts at (1,1)?

4 views (last 30 days)
Renea Young on 21 Nov 2021
Answered: Star Strider on 21 Nov 2021
Can someone help me?
How do I create a diagonal matrix of lambda, where lambda = 0.2? I would like the diagonal to start at (1,1).
Any help would be greatly appreciated.
Renea Young on 21 Nov 2021
The output should be A = [0 0 0 0 0 ....0;0 0.200 0 0 0 .....0;0 0 0.200 0 0...0; 0 0 0 0.200 0 ....0]
The dimension is N

Star Strider on 21 Nov 2021
Without using repmat
n = 4;
lambda = 0.2;
A = diag([0 ones(1,n)]*lambda)
A = 5×5
0 0 0 0 0 0 0.2000 0 0 0 0 0 0.2000 0 0 0 0 0 0.2000 0 0 0 0 0 0.2000
,

Jan on 21 Nov 2021
Maybe you mean:
n = 4;
lambda = 0.2;
A = eye(n) * lambda
A = 4×4
0.2000 0 0 0 0 0.2000 0 0 0 0 0.2000 0 0 0 0 0.2000
% Or:
A = diag(repmat(lambda, 1, n))
A = 4×4
0.2000 0 0 0 0 0.2000 0 0 0 0 0.2000 0 0 0 0 0.2000

Walter Roberson on 21 Nov 2021
n = 4;
lambda = 0.2;
A = diag(repmat(lambda, 1, n), 1)
A = 5×5
0 0.2000 0 0 0 0 0 0.2000 0 0 0 0 0 0.2000 0 0 0 0 0 0.2000 0 0 0 0 0