0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 矩阵相乘

[2,3]  即第二行第三列这个点.

``````> x <- matrix(data = c(1:6), nrow = 2, ncol = 3, byrow = TRUE)
> x
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> y <- matrix(data = c(1:12), nrow = 6, ncol = 2, byrow = TRUE)
> y
[,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[4,]    7    8
[5,]    9   10
[6,]   11   12

> y %*% x
[,1] [,2] [,3]
[1,]    9   12   15
[2,]   19   26   33
[3,]   29   40   51
[4,]   39   54   69
[5,]   49   68   87
[6,]   59   82  105``````

``````> z <- y %*% x
> z[3,2]
[1] 40
> sum(y[3,] * x[,2])
[1] 40
> z[6,3]
[1] 105
> sum(y[6,] * x[,3])
[1] 105

> x[,3]
[1] 3 6
> y[6,]
[1] 11 12
3*11+6*12 = 105``````

v %*% a, 那么v的长度必须等于a的行数. 并且相乘时v转换为(1行多列)的矩阵

a %*% v, 那么a的列数必须等于v的长度. 并且相乘时v转换为(多行1列)的矩阵

``````> a <- c(17,19)
> x
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> a
[1] 17 19``````

``````> x %*% a
Error in x %*% a : non-conformable arguments``````

``````> a %*% x
[,1] [,2] [,3]
[1,]   93  129  165

93 = 1*17 + 4*19
129 = 2*17 + 5*19
165 = 3*17 + 6*19``````

``````> b <- c(100,200,300)
> b %*% x
Error in b %*% x : non-conformable arguments

> x %*% b
[,1]
[1,] 1400
[2,] 3200

1400 = 100*1 + 200*2 + 300*3
3200 = 100*4 + 200*5 + 300*6``````

R提供了crossprod和tcrossprod函数, 也是矩阵相乘, 但是速度比%*%更快.(帮助中提到的)
``````> help(crossprod)
crossprod                 package:base                 R Documentation

Matrix Crossproduct

Description:

Given matrices ‘x’ and ‘y’ as arguments, return a matrix
cross-product.  This is formally equivalent to (but usually
slightly faster than) the call ‘t(x) %*% y’ (‘crossprod’) or ‘x
%*% t(y)’ (‘tcrossprod’).

Usage:

crossprod(x, y = NULL)

tcrossprod(x, y = NULL)

Arguments:

x, y: numeric or complex matrices: ‘y = NULL’ is taken to be the
same matrix as ‘x’.  Vectors are promoted to single-column or
single-row matrices, depending on the context.

Value:

A double or complex matrix, with appropriate ‘dimnames’ taken from
‘x’ and ‘y’.

Note:

When ‘x’ or ‘y’ are not matrices, they are treated as column or
row matrices, but their ‘names’ are usually *not* promoted to
‘dimnames’.  Hence, currently, the last example has empty
dimnames.

‘%*%’ and outer product ‘%o%’.

Examples:

(z <- crossprod(1:4))    # = sum(1 + 2^2 + 3^2 + 4^2)
drop(z)                  # scalar
x <- 1:4; names(x) <- letters[1:4]; x
tcrossprod(as.matrix(x)) # is
identical(tcrossprod(as.matrix(x)),
crossprod(t(x)))
tcrossprod(x)            # no dimnames

m <- matrix(1:6, 2,3) ; v <- 1:3; v2 <- 2:1
stopifnot(identical(tcrossprod(v, m), v %*% t(m)),
identical(tcrossprod(v, m), crossprod(v, t(m))),
identical(crossprod(m, v2), t(m) %*% v2))``````

``````> x <- matrix(1:12,3,4)
> x
[,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> t(x)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12

> x %*% t(x)
[,1] [,2] [,3]
[1,]  166  188  210
[2,]  188  214  240
[3,]  210  240  270
> t(x) %*% x
[,1] [,2] [,3] [,4]
[1,]   14   32   50   68
[2,]   32   77  122  167
[3,]   50  122  194  266
[4,]   68  167  266  365

> crossprod(x,x)
[,1] [,2] [,3] [,4]
[1,]   14   32   50   68
[2,]   32   77  122  167
[3,]   50  122  194  266
[4,]   68  167  266  365
> tcrossprod(x,x)
[,1] [,2] [,3]
[1,]  166  188  210
[2,]  188  214  240
[3,]  210  240  270``````

crossprod, tcrossprod的y可以不输入.
``````> crossprod(x)
[,1] [,2] [,3] [,4]
[1,]   14   32   50   68
[2,]   32   77  122  167
[3,]   50  122  194  266
[4,]   68  167  266  365
> tcrossprod(x)
[,1] [,2] [,3]
[1,]  166  188  210
[2,]  188  214  240
[3,]  210  240  270``````

``````> crossprod(x,x) == t(x) %*% x
[,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE
[4,] TRUE TRUE TRUE TRUE
> tcrossprod(x,x) == x %*% t(x)
[,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE``````

``````> help(t)
t                     package:base                     R Documentation

Matrix Transpose

Description:

Given a matrix or ‘data.frame’ ‘x’, ‘t’ returns the transpose of
‘x’.

Usage:

t(x)

Arguments:

x: a matrix or data frame, typically.

Details:

This is a generic function for which methods can be written.  The
description here applies to the default and ‘"data.frame"’
methods.

A data frame is first coerced to a matrix: see ‘as.matrix’.  When
‘x’ is a vector, it is treated as a column, i.e., the result is a
1-row matrix.

Value:

A matrix, with ‘dim’ and ‘dimnames’ constructed appropriately from
those of ‘x’, and other attributes except names copied across.``````

[参考]
5. R

``````> help("%*%")
matmult                  package:base                  R Documentation

Matrix Multiplication

Description:

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 it
will return the inner product (as a matrix).

Usage:

x %*% y

Arguments:

x, y: numeric or complex matrices or vectors.

Details:

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

This operator is S4 generic but not S3 generic.  S4 methods need
to be written for a function of two arguments named ‘x’ and ‘y’.

Value:

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

References:

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S

‘matrix’, ‘Arithmetic’, ‘diag’.

Examples:

x <- 1:4
(z <- x %*% x)    # scalar ("inner") product (1 x 1 matrix)
drop(z)             # as scalar

y <- diag(x)
z <- matrix(1:12, ncol = 3, nrow = 4)
y %*% z
y %*% x
x %*% z``````

+ 关注