MATLAB: Fast question!

matrixsparse

Hi all,
How do I make this matrix in Matlab?
Thanks

Best Answer

  • If the output matrix is to be n by n, then
    M = diag(ones(m-1,1),1) + diag(-ones(m-1,1),-1) + diag(1,1-n) + diag(1,n-1);