Method on @sym: M = diag (v)
Method on @sym: M = diag (v, k)
Method on @sym: M = diag (v, n, m)
Method on @sym: v = diag (A)
Method on @sym: v = diag (A, k)

Make diagonal matrix or extract diagonal of symbolic matrix.

Example:

A = sym([1 2 3; 4 5 6; 7 8 9])
  ⇒ A = (sym 3×3 matrix)
      ⎡1  2  3⎤
      ⎢       ⎥
      ⎢4  5  6⎥
      ⎢       ⎥
      ⎣7  8  9⎦

v = diag(A)
  ⇒ v = (sym 3×1 matrix)
      ⎡1⎤
      ⎢ ⎥
      ⎢5⎥
      ⎢ ⎥
      ⎣9⎦

Make it back into a matrix:

diag(v)
  ⇒ (sym 3×3 matrix)
      ⎡1  0  0⎤
      ⎢       ⎥
      ⎢0  5  0⎥
      ⎢       ⎥
      ⎣0  0  9⎦

See also: @sym/repmat.

Package: symbolic