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:
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?
Like this?
*** EDIT ***
If you want to alter a pre-existing matrix you could just use linear indexing:
This even works for non-square matrices. E.g.,