Main Content

symmatrix

Create symbolic matrix variable

Since R2021a

Description

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.

example

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

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

example

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 = A
B = symmatrix('B',[2 3])
B = B

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+B

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-BA
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 = A
X = symmatrix('X',[3 1])
X = X

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+A

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 = 

Σ1where  Σ1=(1121314121314151314151614151617)

class(X)
ans = 
'symmatrix'

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

A = symmatrix('A',2)
A = A
B = symmatrix('B',2)
B = B

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

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)

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

  • 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.

Version History

Introduced in R2021a