Main Content

symmatrix

Create symbolic matrix variable

Description

example

X = symmatrix('X',[nrow ncol]) creates an nrow-by-ncol symbolic matrix variable X. Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. For more information, see Create Symbolic Matrix Variables.

example

X = symmatrix('X',n) creates an n-by-n symbolic matrix variable X.

X = symmatrix('X') creates a 1-by-1 symbolic matrix variable X.

example

X = symmatrix(S) converts a numeric matrix or a matrix of symbolic scalar variables specified by S to a symbolic matrix variable X.

Examples

collapse all

Create two symbolic matrix variables with size 2-by-3. Nonscalar symbolic matrix variables are displayed as bold characters in the Live Editor and Command Window.

A = symmatrix('A',[2 3])
A = Asymmatrix('A', [2 3])
B = symmatrix('B',[2 3])
B = Bsymmatrix('B', [2 3])

Add the two matrices. The summation of the two symbolic matrix variables is denoted by the matrix notation A+B.

X = A + B
X = A+Bsymmatrix('A', [2 3]) + symmatrix('B', [2 3])

Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. When representing nonscalars, these variables are noncommutative. When mathematical formulas involve matrices and vectors, writing them using symbolic matrix variables is more concise and clear than writing them componentwise.

Create two symbolic matrix variables.

A = symmatrix('A',[2 2]);
B = symmatrix('B',[2 2]);

Check the commutation relation for multiplication between two symbolic matrix variables.

A*B - B*A
ans = AB-BAsymmatrix('A', [2 2])*symmatrix('B', [2 2]) - symmatrix('B', [2 2])*symmatrix('A', [2 2])
isequal(A*B,B*A)
ans = logical
   0

Check the commutation relation for addition between two symbolic matrix variables.

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

Create 3-by-3 and 3-by-1 symbolic matrix variables.

A = symmatrix('A',3)
A = Asymmatrix('A', [3 3])
X = symmatrix('X',[3 1])
X = Xsymmatrix('X', [3 1])

Find the Hessian matrix of XTAX. Derived equations involving symbolic matrix variables are displayed in typeset as they would be in textbooks.

f = X.'*A*X;
H = diff(f,X,X.')
H = AT+Atranspose(symmatrix('A', [3 3])) + symmatrix('A', [3 3])

Create a Hilbert matrix of order 4. The data type of the matrix is double.

H = hilb(4)
H = 4×4

    1.0000    0.5000    0.3333    0.2500
    0.5000    0.3333    0.2500    0.2000
    0.3333    0.2500    0.2000    0.1667
    0.2500    0.2000    0.1667    0.1429

class(H)
ans = 
'double'

Convert the numeric matrix to a symbolic matrix variable. The data type of the converted matrix is symmatrix.

X = symmatrix(H)
X = 

(1121314121314151314151614151617)[sym(1), sym(1/2), sym(1/3), sym(1/4); sym(1/2), sym(1/3), sym(1/4), sym(1/5); sym(1/3), sym(1/4), sym(1/5), sym(1/6); sym(1/4), sym(1/5), sym(1/6), sym(1/7)]

class(X)
ans = 
'symmatrix'

Create two symbolic matrix variables with size 2-by-2.

A = symmatrix('A',2)
A = Asymmatrix('A', [2 2])
B = symmatrix('B',2)
B = Bsymmatrix('B', [2 2])

Perform matrix multiplication between A and B. The multiplication of the two symbolic matrix variables is represented by the matrix notation AB.

X = A*B
X = ABsymmatrix('A', [2 2])*symmatrix('B', [2 2])

Convert the symbolic matrix variable X to a matrix of symbolic scalar variables S. The multiplication of two matrices of symbolic scalar variables is represented by the elements of the matrix product.

S = symmatrix2sym(X)
S = 

(A1,1B1,1+A1,2B2,1A1,1B1,2+A1,2B2,2A2,1B1,1+A2,2B2,1A2,1B1,2+A2,2B2,2)[A1_1*B1_1 + A1_2*B2_1, A1_1*B1_2 + A1_2*B2_2; A2_1*B1_1 + A2_2*B2_1, A2_1*B1_2 + A2_2*B2_2]

Input Arguments

collapse all

Variable name, specified as a character vector. Argument X must be a valid variable name. That is, X must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use isvarname.

Example: x, y12, z_1

Vector or matrix dimensions, specified as a vector of integers. nrow is the number of rows, and ncol is the number of columns. As a shortcut, you can create a square symbolic matrix variable by specifying only one integer. For example, X = symmatrix('X',3) creates a square 3-by-3 symbolic matrix variable.

Example: [2 3], [2,3]

Numeric matrix or matrix of symbolic scalar variables to be converted to symbolic matrix variable, specified as a number, numeric matrix, symbolic scalar variable, or a matrix of symbolic scalar variables.

Example: 10, eye(3), pi, hilb(3)

Limitations

  • Using Symbolic Math Toolbox™, you can create symbolic functions that depend on symbolic scalar variables as parameters. However, symbolic matrix variables cannot be parameter-dependent. For example, the command symmatrix('A(x)',[3 2]) currently errors.

  • Differentiation functions, such as jacobian and laplacian, currently do not accept symbolic matrix variables as input. To evaluate differentiation with respect to vectors and matrices, you can use the diff function instead.

  • To show all the functions in Symbolic Math Toolbox that accept symbolic matrix variables as input, use the command methods symmatrix.

Alternative Functionality

Alternative Approaches for Creating Symbolic Matrix Variables

To create several symbolic matrix variables in one function call, use syms var1 ... varN [nrow ncol] matrix. For more details, see syms.

Introduced in R2021a