Main Content

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 A, x, and y by using syms. Find the differential of the expression yTAx with respect to the vector x.

syms A [3 4] matrix
syms x [4 1] matrix
syms y [3 1] matrix
eq = y.'*A*x
eq = yTAx
D = diff(eq,x)
D = yTA

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 2-by-3 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 = 

(A1,1A1,2A1,3A2,1A2,2A2,3)

B
B = 

(B1,1B1,2B1,3B2,1B2,2B2,3)

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 = 

(A1,1B1,1+A1,2B1,2+A1,3B1,3A1,1B2,1+A1,2B2,2+A1,3B2,3A2,1B1,1+A2,2B1,2+A2,3B1,3A2,1B2,1+A2,2B2,2+A2,3B2,3)

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 = ABT

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 = AB-BA

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 = cA
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 = VTAV
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 = VTAT+VTA

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 = 

Σ1where  Σ1=(A1,1A1,2A1,3A1,4A2,1A2,2A2,3A2,4A3,1A3,2A3,3A3,4)

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 = AB
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 = 

(A1,1B1,1+A1,2B2,1A1,1B1,2+A1,2B2,2A2,1B1,1+A2,2B2,1A2,1B1,2+A2,2B2,2)

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 = A2,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 = 

(A1,1A1,2A1,3A2,1A2,2A2,3)

asym = Asym(2,3)
asym = A2,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 = I3
symmatrix(zeros(2,3))
ans = 02,3
symmatrix(ones(3,5))
ans = 13,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 = AB
A./B
ans = AB
A.\B
ans = BA
A.*hilb(3)
ans = 

AΣ1where  Σ1=(11213121314131415)

A.^(2*ones(3))
ans = A213,3
A.^B
ans = AB
kron(A,B)
ans = AB
adjoint(A)
ans = adj(A)
trace(A)
ans = Tr(A)

See Also

| |

Related Topics