symmatrix2sym

Convert symbolic matrix variable to array of scalar variables

Description

example

S = symmatrix2sym(M) converts a symbolic matrix variable M of type symmatrix to an array of symbolic scalar variables S of type sym.

The output array is the same size as the input symbolic matrix variable and its components are filled with automatically generated elements. For example, syms M [1 3] matrix; S = symmatrix2sym(M) creates the matrix S = [M1_1, M1_2, M1_3]. The generated elements M1_1, M1_2, and M1_3 do not appear in the MATLAB® workspace.

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.

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

Add the two matrices. The result is represented by the matrix notation $\text{A}+\text{B}$.

X = A + B
X = $A+B$

The data type of X is symmatrix.

class(X)
ans =
'symmatrix'

Convert the symbolic matrix variable X to a matrix of symbolic scalar variables Y. The result is denoted by the sum of the matrix components.

Y = symmatrix2sym(X)
Y =

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

The data type of Y is sym.

class(Y)
ans =
'sym'

Show that the converted result in Y is equal to the sum of two matrices of symbolic scalar variables.

syms A B [2 3]
Y2 = A + B
Y2 =

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

isequal(Y,Y2)
ans = logical
1

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

syms A [3 3] matrix
syms X [3 1] matrix

Find the Hessian matrix of ${\text{X}}^{T}\text{A}\text{X}$.

f = X.'*A*X;
H = diff(f,X,X.')
H = ${A}^{\mathrm{T}}+A$

Convert the result from a symbolic matrix variable H to a matrix of symbolic scalar variables S.

S = symmatrix2sym(H)
S =

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

Create a 1-by-3 symbolic matrix variable that represents a vector.

syms A [1 3] matrix

Find the 2-norm of the vector A. The result is a symbolic matrix variable with symmatrix data type.

N = norm(A)
N = ${‖A‖}_{2}$
class(N)
ans =
'symmatrix'

Convert N to a symbolic scalar variable to express the 2-norm in terms of the components of A. The result is a symbolic scalar variable with sym data type.

N = symmatrix2sym(N)
N = $\sqrt{{|{A}_{1,1}|}^{2}+{|{A}_{1,2}|}^{2}+{|{A}_{1,3}|}^{2}}$
class(N)
ans =
'sym'

Create two vectors of size 3-by-1 as symbolic matrix variables.

syms A B [3 1] matrix

Find the dot product of the two vectors by evaluating transpose(A)*B.

C = transpose(A)*B
C = ${A}^{\mathrm{T}} B$

Convert C to a symbolic scalar variable to express the dot product in terms of the components of A and B.

C = symmatrix2sym(C)
C = ${A}_{1} {B}_{1}+{A}_{2} {B}_{2}+{A}_{3} {B}_{3}$

Create two 2-by-3 symbolic matrix variables.

syms A B [2 3] matrix

Concatenate the two matrices vertically using the command vertcat(A,B) or [A; B].

C = [A; B]
C =

$\left(\begin{array}{c}A\\ B\end{array}\right)$

Convert C to a matrix of symbolic scalar variables.

C = symmatrix2sym(C)
C =

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

Input Arguments

collapse all

Input, specified as a symbolic matrix variable.

Data Types: symmatrix

Tips

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

Introduced in R2021a

Symbolic Math Toolbox Documentation

Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos