Main Content

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

Add the two matrices. The result is represented by the matrix notation A+B.

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

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 = 

(A1,1+B1,1A1,2+B1,2A1,3+B1,3A2,1+B2,1A2,2+B2,2A2,3+B2,3)[A1_1 + B1_1, A1_2 + B1_2, A1_3 + B1_3; A2_1 + B2_1, A2_2 + B2_2, A2_3 + B2_3]

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 = 

(A1,1+B1,1A1,2+B1,2A1,3+B1,3A2,1+B2,1A2,2+B2,2A2,3+B2,3)[A1_1 + B1_1, A1_2 + B1_2, A1_3 + B1_3; A2_1 + B2_1, A2_2 + B2_2, A2_3 + B2_3]

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

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

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

S = symmatrix2sym(H)
S = 

(2A1,1A1,2+A2,1A1,3+A3,1A1,2+A2,12A2,2A2,3+A3,2A1,3+A3,1A2,3+A3,22A3,3)[2*A1_1, A1_2 + A2_1, A1_3 + A3_1; A1_2 + A2_1, 2*A2_2, A2_3 + A3_2; A1_3 + A3_1, A2_3 + A3_2, 2*A3_3]

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 = A2norm(symmatrix('A', [1 3]), 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 = |A1,1|2+|A1,2|2+|A1,3|2sqrt(abs(A1_1)^2 + abs(A1_2)^2 + abs(A1_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 = ATBtranspose(symmatrix('A', [3 1]))*symmatrix('B', [3 1])

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 = (A1B1+A2B2+A3B3)[A1*B1 + A2*B2 + A3*B3]

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 = 

(AB)[symmatrix('A', [2 3]); symmatrix('B', [2 3])]

Convert C to a matrix of symbolic scalar variables.

C = symmatrix2sym(C)
C = 

(A1,1A1,2A1,3A2,1A2,2A2,3B1,1B1,2B1,3B2,1B2,2B2,3)[A1_1, A1_2, A1_3; A2_1, A2_2, A2_3; B1_1, B1_2, B1_3; B2_1, B2_2, B2_3]

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