Is there any inline command to generate shifted identity matrix in MATLAB?
A=[ ... 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 1, 0, 0
You can get the desired output with a single call to bsxfun -
n = 10 shift = 1 A = bsxfun(@eq,[1:n].',1-shift:n-shift)
Since you are basically creating a sparse matrix, alternatively you can use sparse -
n = 10 shift = 1 A = full(sparse(1:n-shift,1+shift:n,1,n,n))