Create diagonal matrix or get diagonals from symbolic matrices
Create Matrix with Diagonal as Vector
Create a symbolic matrix with the main diagonal specified by the vector
syms a b c v = [a b c]; diag(v)
ans = [ a, 0, 0] [ 0, b, 0] [ 0, 0, c]
Create Matrix with Subdiagonal as Vector
Create a symbolic matrix with the second diagonal below the main diagonal
specified by the vector
syms a b c v = [a b c]; diag(v,-2)
ans = [ 0, 0, 0, 0, 0] [ 0, 0, 0, 0, 0] [ a, 0, 0, 0, 0] [ 0, b, 0, 0, 0] [ 0, 0, c, 0, 0]
Extract Diagonal from Matrix
Extract the main diagonal from a square matrix.
syms x y z A = magic(3).*[x, y, z]; diag(A)
ans = 8*x 5*y 2*z
Extract Superdiagonal from Matrix
Extract the first diagonal above the main diagonal.
syms x y z A = magic(3).*[x, y, z]; diag(A,1)
ans = y 7*z
v — Diagonal elements
Diagonal elements, specified as a symbolic vector. If
v is a vector
N elements, then
diag(v,k) is a square matrix of
N + abs(k).
A — Input matrix
Input matrix, specified as a symbolic matrix.
k — Diagonal number
Diagonal number, specified as an integer.
k = 0 represents the main
k > 0 is above the main diagonal, and
k < 0
is below the main diagonal.
traceof a matrix is equal to