Populate Lower Sub-diagonal of a Square Matrix with Specified Value MATLAB

57 Views Asked by At

I have an N by N matrix W_res,DLR of all zeros. I would like to populate just the lower sub diagonal elements with the scalar value r. For example, if N=4, the matrix should look like: enter image description here I thought this would work:

r = 0.5;
W_res,DLR = zeros(N,N);
W_res,DLR(logical(diag(ones(1,N-1),-1))) = r*ones(1,N-1)

but this is not the case. W_res,DLR is a matrix of all NaNs. Where am I going wrong?

2

There are 2 best solutions below

2
James Tursa On BEST ANSWER

Like this?

>> N = 4;
>> r = 0.5;
>> diag(r*ones(N-1,1),-1)
ans =
         0         0         0         0
    0.5000         0         0         0
         0    0.5000         0         0
         0         0    0.5000         0

*** EDIT ***

If you want to alter a pre-existing matrix you could just use linear indexing:

>> DLR = ones(N,N)*7 % start with arbitrary matrix
DLR =
     7     7     7     7
     7     7     7     7
     7     7     7     7
     7     7     7     7
>> N = size(DLR,1); % get the row size
>> DLR(2:N+1:N*(N-1)) = r % linear indexing the lower diagonal
DLR =
    7.0000    7.0000    7.0000    7.0000
    0.5000    7.0000    7.0000    7.0000
    7.0000    0.5000    7.0000    7.0000
    7.0000    7.0000    0.5000    7.0000

This even works for non-square matrices. E.g.,

>> X = 8*ones(5,7) % start with arbitrary non-square matrix
X =
     8     8     8     8     8     8     8
     8     8     8     8     8     8     8
     8     8     8     8     8     8     8
     8     8     8     8     8     8     8
     8     8     8     8     8     8     8
>> N = size(X,1); % get the row size
>> X(2:N+1:N*(N-1)) = r % linear indexing the lower diagonal
X =
    8.0000    8.0000    8.0000    8.0000    8.0000    8.0000    8.0000
    0.5000    8.0000    8.0000    8.0000    8.0000    8.0000    8.0000
    8.0000    0.5000    8.0000    8.0000    8.0000    8.0000    8.0000
    8.0000    8.0000    0.5000    8.0000    8.0000    8.0000    8.0000
    8.0000    8.0000    8.0000    0.5000    8.0000    8.0000    8.0000
4
Luis Mendo On

To write the value r into one of the diagonals of a square matrix DLR without modifying other entries, you can make use of implicit expansion and logical indexing as follows:

diag_index = -1; % 0 is main diagonal, -1 is right below that, etc
DLR((1:size(DLR,1))-(1:size(DLR,2)).'==diag_index) = r;

Complete example:

DLR = randi(9, 5);   % square matrix of size 5 
diag_index = -1;     % -1: diagonal right below the main one
r = 10;              % value to write into that diagonal
DLR((1:size(DLR,1))-(1:size(DLR,2)).'==diag_index) = r;
DLR =
     1     4     5     5     9
    10     1     2     7     6
     2    10     1     1     4
     4     2    10     3     3
     7     6     7    10     7

The same code works for writing a vector of values into the diagonal:

DLR = randi(9, 5);   % square matrix of size 5 
diag_index = -1;     % -1: diagonal right below the main one
r = [10 20 30 40];   % vector of values to write into that diagonal
DLR((1:size(DLR,1))-(1:size(DLR,2)).'==diag_index) = r;
DLR =
     8     1     2     2     6
    10     3     9     4     1
     2    20     9     9     8
     9     9    30     8     9
     6     9     8    40     7