Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2023.0.0 - English


diagonal including or extracting


y = diag(vm)
y = diag(vm, k)



vector or matrix (full or sparse storage)


integer (default value 0)


vector or matrix


for vm a (row or column) n-vector diag(vm) returns a diagonal matrix with entries of vm along the main diagonal.

diag(vm,k) is a (n+abs(k))x(n+abs(k)) matrix with the entries of vm along the kth diagonal. k=0 is the main diagonal k>0 is for upper diagonals and k<0 for lower diagonals.

For a matrix vm, diag(vm,k) is the column vector made of entries of the kth diagonal of vm. diag(vm) is the main diagonal of vm. diag(diag(x)) is a diagonal matrix.

If vm is a sparse matrix diag(vm,k) returns a sparse matrix.

To construct a diagonal linear system, use blockdiag.

Note that eye(A).*A returns a diagonal matrix made with the diagonal entries of A. This is valid for any matrix (constant, polynomial, rational, state-space linear system,...).



diag(A)  // main diagonal

diag(sparse(1:10))  // sparse diagonal matrix

// form a tridiagonal matrix of size 2*m+1
m=5;diag(-m:m) +  diag(ones(2*m,1),1) +diag(ones(2*m,1),-1)

See also

  • toeplitz — Toeplitz matrix (chosen constant diagonal bands)
  • eye — identity matrix
  • blockdiag — Creates a block diagonal matrix from provided arrays. Block diagonal system connection.
  • spec — eigenvalues, and eigenvectors of a matrix or a pencil
  • bdiag — block diagonalization, generalized eigenvectors
Report an issue
<< blockdiag Matrix generation empty [] >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue Mar 07 09:28:41 CET 2023