Matlab identity shift matrix

前端 未结 6 1210
长发绾君心
长发绾君心 2020-12-17 18:38

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         


        
6条回答
  •  时光说笑
    2020-12-17 19:26

    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))
    

提交回复
热议问题