# Create Symbolic Matrix Variables

Since R2021a

Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. When mathematical formulas involve matrices and vectors, writing them using symbolic matrix variables is more concise and clear than writing them componentwise. When you do this, you can take vector-based expressions and equations from textbooks, enter them in Symbolic Math Toolbox™, perform mathematical operations on them, and derive further equations from them.

Derived equations involving symbolic matrix variables are displayed in typeset as they would be in textbooks. For example, create three symbolic matrix variables $\text{A}$, $\text{x}$, and $\text{y}$ by using syms. Find the differential of the expression ${\text{y}}^{T}\text{A}\text{x}$ with respect to the vector $\text{x}$.

syms A [3 4] matrix
syms x [4 1] matrix
syms y [3 1] matrix
eq = y.'*A*x
eq = ${y}^{\mathrm{T}} A x$
D = diff(eq,x)
D = ${y}^{\mathrm{T}} A$

### Comparison Between Matrix of Symbolic Scalar Variables and Symbolic Matrix Variables

Symbolic matrix variables are an alternative to symbolic scalar variables. The two options are of different types and displayed differently.

For example, create two 3-by-4 matrices of symbolic scalar variables by using syms. For brevity, matrices of symbolic scalar variables are sometimes called symbolic matrices. These matrices are displayed by listing their components.

syms A B [2 3]
A
A =

$\left(\begin{array}{ccc}{A}_{1,1}& {A}_{1,2}& {A}_{1,3}\\ {A}_{2,1}& {A}_{2,2}& {A}_{2,3}\end{array}\right)$

B
B =

$\left(\begin{array}{ccc}{B}_{1,1}& {B}_{1,2}& {B}_{1,3}\\ {B}_{2,1}& {B}_{2,2}& {B}_{2,3}\end{array}\right)$

A matrix of symbolic scalar variables is of type sym.

class(A)
ans =
'sym'

Applying symbolic math operations to these matrices can result in a complex solution expressed in terms of the matrix components. For example, multiply the matrices A and B'.

C = A*B'
C =

$\left(\begin{array}{cc}{A}_{1,1} \stackrel{‾}{{B}_{1,1}}+{A}_{1,2} \stackrel{‾}{{B}_{1,2}}+{A}_{1,3} \stackrel{‾}{{B}_{1,3}}& {A}_{1,1} \stackrel{‾}{{B}_{2,1}}+{A}_{1,2} \stackrel{‾}{{B}_{2,2}}+{A}_{1,3} \stackrel{‾}{{B}_{2,3}}\\ {A}_{2,1} \stackrel{‾}{{B}_{1,1}}+{A}_{2,2} \stackrel{‾}{{B}_{1,2}}+{A}_{2,3} \stackrel{‾}{{B}_{1,3}}& {A}_{2,1} \stackrel{‾}{{B}_{2,1}}+{A}_{2,2} \stackrel{‾}{{B}_{2,2}}+{A}_{2,3} \stackrel{‾}{{B}_{2,3}}\end{array}\right)$

To create symbolic matrix variables of the same size, use the syms command followed by the variable names, their size, and the matrix keyword. Symbolic matrix variables are displayed in bold to distinguish them from symbolic scalar variables.

syms A B [2 3] matrix
A
A = $A$
B
B = $B$

Symbolic matrix variables are of type symmatrix.

class(A)
ans =
'symmatrix'

Applying symbolic math operations to symbolic matrix variables results in a concise display. For example, multiply A and B'.

C = A*B'
C = $A {\left(\stackrel{‾}{B}\right)}^{\mathrm{T}}$

### Mathematical Operations with Symbolic Matrix Variables

Symbolic matrix variables are recognized as noncommutative objects. They support common math operations, and you can use these operations to build symbolic matrix variable expressions.

syms A B [2 2] matrix
A*B - B*A
ans = $A B-B A$

For example, check the commutation relation for multiplication between two symbolic matrix variables.

isequal(A*B,B*A)
ans = logical
0

Check the commutation relation for addition.

isequal(A+B,B+A)
ans = logical
1

If an operation has any arguments of type symmatrix, the result is automatically converted to type symmatrix. For example, multiply a matrix A that is represented by symbolic matrix variable and a scalar c that is represented by symbolic scalar variable. The result is of type symmatrix.

syms A [2 2] matrix
syms c
class(A)
ans =
'symmatrix'
class(c)
ans =
'sym'
M = c*A
M = $c A$
class(M)
ans =
'symmatrix'

Multiply three matrices that are represented by symbolic matrix variables. The result X is a symmatrix object.

syms V [2 1] matrix
X = V.'*A*V
X = ${V}^{\mathrm{T}} A V$
class(X)
ans =
'symmatrix'

You can pass symmatrix objects as arguments to math functions. For example, perform a mathematical operation to X by taking the differential of X with respect to V.

diff(X,V)
ans = ${V}^{\mathrm{T}} {A}^{\mathrm{T}}+{V}^{\mathrm{T}} A$

### Create Symbolic Matrix Variable from Array of Symbolic Scalar Variables

You can convert an array of symbolic scalar variables to a single symbolic matrix variable using the symmatrix function. Symbolic matrix variables that are converted in this way are displayed elementwise.

syms A [3 4]
class(A)
ans =
'sym'
B = symmatrix(A)
B =

class(B)
ans =
'symmatrix'

### Convert Symbolic Matrix Variable into Array of Symbolic Scalar Variables

You can create symbolic matrix variables, derive equations, and then convert the result to arrays of symbolic scalar variables using the symmatrix2sym function.

For example, find the matrix product of two symbolic matrix variables A and B. The result X is of type symmatrix.

syms A B [2 2] matrix
X = A*B
X = $A B$
class(X)
ans =
'symmatrix'

Convert the symbolic matrix variable X to array of symbolic scalar variables. The converted matrix Y is of type sym.

Y = symmatrix2sym(X)
Y =

$\left(\begin{array}{cc}{A}_{1,1} {B}_{1,1}+{A}_{1,2} {B}_{2,1}& {A}_{1,1} {B}_{1,2}+{A}_{1,2} {B}_{2,2}\\ {A}_{2,1} {B}_{1,1}+{A}_{2,2} {B}_{2,1}& {A}_{2,1} {B}_{1,2}+{A}_{2,2} {B}_{2,2}\end{array}\right)$

class(Y)
ans =
'sym'

Check that the product obtained by converting symbolic matrix variables is equal to the product of two arrays of symbolic scalar variables.

syms A B [2 2]
isequal(Y,A*B)
ans = logical
1

### Indexing into Symbolic Matrix Variables

Indexing into a symbolic matrix variable returns corresponding matrix elements in the form of another symbolic matrix variable.

syms A [2 3] matrix
a = A(2,3)
a = ${A}_{2,3}$
class(a)
ans =
'symmatrix'

Alternatively, convert the symbolic matrix variable A to a matrix of symbolic scalar variables. Then, index into that matrix.

Asym = symmatrix2sym(A)
Asym =

$\left(\begin{array}{ccc}{A}_{1,1}& {A}_{1,2}& {A}_{1,3}\\ {A}_{2,1}& {A}_{2,2}& {A}_{2,3}\end{array}\right)$

asym = Asym(2,3)
asym = ${A}_{2,3}$
class(asym)
ans =
'sym'

Note that both results are equal.

isequal(a,symmatrix(asym))
ans = logical
1

### Display of Operations Involving Symbolic Matrix Variables

Matrices like those returned by eye, zeros, and ones often have special meaning with specific notation in symbolic workflows. Declaring these matrices as symbolic matrix variables display the matrices in bold along with the matrix dimensions.

symmatrix(eye(3))
ans = ${\mathrm{I}}_{3}$
symmatrix(zeros(2,3))
ans = ${\mathrm{0}}_{2,3}$
symmatrix(ones(3,5))
ans = ${\mathrm{1}}_{3,5}$

If the inputs to a componentwise operation in MATLAB® are symbolic matrix variables, so is the output. These operations are displayed in special notations which follow conventions from textbooks.

syms A B [3 3] matrix
A.*B
ans = $A\odot B$
A./B
ans = $A\oslash B$
A.\B
ans = $B\oslash A$
A.*hilb(3)
ans =

A.^(2*ones(3))
ans = ${A}^{\circ 2 {\mathrm{1}}_{3,3}}$
A.^B
ans = ${A}^{\circ B}$
kron(A,B)
ans = $A\otimes B$
ans = $\mathrm{adj}\left(A\right)$
ans = $\mathrm{Tr}\left(A\right)$