Skip to contents

Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

Usage

# S3 method for default
%*%(x, y)

Arguments

x, y

numeric or complex matrices or vectors.

Value

A double or complex matrix product. Use drop to remove dimensions which have only one level.

Details

When a vector is promoted to a matrix, its names are not promoted to row or column names, unlike as.matrix.

Promotion of a vector to a 1-row or 1-column matrix happens when one of the two choices allows x and y to get conformable dimensions.

This operator is a generic function: methods can be written for it individually or via the matOps group generic function; it dispatches to S3 and S4 methods. Methods need to be written for a function that takes two arguments named x and y.

See also

Author

R core team and contributors

Examples

x <- 1:4
(z <- x %*% x)    # scalar ("inner") product (1 x 1 matrix)
#>      [,1]
#> [1,]   30
drop(z)             # as scalar
#> [1] 30

y <- diag(x)
z <- matrix(1:12, ncol = 3, nrow = 4)
y %*% z
#>      [,1] [,2] [,3]
#> [1,]    1    5    9
#> [2,]    4   12   20
#> [3,]    9   21   33
#> [4,]   16   32   48
y %*% x
#>      [,1]
#> [1,]    1
#> [2,]    4
#> [3,]    9
#> [4,]   16
x %*% z
#>      [,1] [,2] [,3]
#> [1,]   30   70  110