Main Content

mtimes,*

Symbolic matrix multiplication

Syntax

Description

example

A*B矩阵乘积的吗AandB. IfAis anm-by-pandBis ap-by-nmatrix, then the result is anm-by-nmatrixCdefined as

C ( i , j ) = k = 1 p A ( i , k ) B ( k , j )

For nonscalarAandB, the number of columns ofAmust equal the number of rows ofB. Matrix multiplication is not universally commutative for nonscalar inputs. That is, typicallyA*Bis not equal toB*A. If at least one input is scalar, thenA*B是equivalent toA.*Band is commutative.

mtimes(A,B)是equivalent toA*B.

Examples

Multiply Two Vectors

Create a1-by-5row vector and a5-by-1column vector.

syms x A = [x, 2*x^2, 3*x^3, 4*x^4] B = [1/x; 2/x^2; 3/x^3; 4/x^4]
A = [ x, 2*x^2, 3*x^3, 4*x^4] B = 1/x 2/x^2 3/x^3 4/x^4

Find the matrix product of these two vectors.

A*B
ans = 30

Multiply Two Matrices

Create a4-by-3matrix and a3-by-2matrix.

A = sym('a%d%d', [4 3]) B = sym('b%d%d', [3 2])
A = [ a11, a12, a13] [ a21, a22, a23] [ a31, a32, a33] [ a41, a42, a43] B = [ b11, b12] [ b21, b22] [ b31, b32]

MultiplyAbyB.

A*B
ans = [ a11*b11 + a12*b21 + a13*b31, a11*b12 + a12*b22 + a13*b32] [ a21*b11 + a22*b21 + a23*b31, a21*b12 + a22*b22 + a23*b32] [ a31*b11 + a32*b21 + a33*b31, a31*b12 + a32*b22 + a33*b32] [ a41*b11 + a42*b21 + a43*b31, a41*b12 + a42*b22 + a43*b32]

Multiply Matrix by Scalar

Create a4-by-4Hilbert matrixH.

H = sym(hilb(4))
H = [ 1, 1/2, 1/3, 1/4] [ 1/2, 1/3, 1/4, 1/5] [ 1/3, 1/4, 1/5, 1/6] [ 1/4, 1/5, 1/6, 1/7]

MultiplyHbyeπ.

C = H*exp(sym(pi))
C = [ exp(pi), exp(pi)/2, exp(pi)/3, exp(pi)/4] [ exp(pi)/2, exp(pi)/3, exp(pi)/4, exp(pi)/5] [ exp(pi)/3, exp(pi)/4, exp(pi)/5, exp(pi)/6] [ exp(pi)/4, exp(pi)/5, exp(pi)/6, exp(pi)/7]

Usevpaanddigitsto approximate symbolic results with the required number of digits. For example, approximate it with five-digit accuracy.

old = digits(5); vpa(C) digits(old)
ans = [ 23.141, 11.57, 7.7136, 5.7852] [ 11.57, 7.7136, 5.7852, 4.6281] [ 7.7136, 5.7852, 4.6281, 3.8568] [ 5.7852, 4.6281, 3.8568, 3.3058]

Input Arguments

collapse all

Input, specified as a symbolic number, scalar variable, matrix variable(since R2021a), function, expression, or vector or matrix of symbolic scalar variables. InputsAandBmust be the same size unless one is a scalar. A scalar value expands into an array of the same size as the other input.

Input, specified as a symbolic number, scalar variable, matrix variable(since R2021a), function, expression, or vector or matrix of symbolic scalar variables. InputsAandBmust be the same size unless one is a scalar. A scalar value expands into an array of the same size as the other input.

Introduced before R2006a