矩阵相乘

在R中矩阵相乘使用%*%运算符号.
矩阵相乘必须满足 : 左边的矩阵列数和右边的矩阵行数相等. 
例如m行,n列的矩阵和n行p列的矩阵可以相乘, 并且得到的是m行,p列的矩阵.
那么结果矩阵上的每个点的数据是怎么计算来的呢?
例如 : 
[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的长度必须等于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

如果左边矩阵的列宽和右边向量的长度不一致, 则无法计算.
反过来, 把向量放在左边, 矩阵放在右边, 那么必须满足左边的向量长度和右边矩阵的行宽要一致.
例子1 : 

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

以下, 左边向量的长度 等于右边矩阵的行宽 (向量转换为1行2列矩阵)
相当于转换为1行2列乘以2行3列.  [1,2] %*% [2,3] ,  得到的是1行3列的矩阵.

> a %*% x
     [,1] [,2] [,3]
[1,]   93  129  165
验证
93 = 1*17 + 4*19
129 = 2*17 + 5*19
165 = 3*17 + 6*19


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

以下, 左边矩阵的列宽等于右边向量的长度(向量转换为3行1列矩阵). 
相当于转换为2行3列乘以3行1列. [2,3] %*% [3,1] , 得到的是2行1列的矩阵.
> 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.

See Also:

     ‘%*%’ 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))


行列变换使用 t() 函数.
> 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.


[参考]
1.  http://blog.csdn.net/myan/article/details/647511 4.  http://f.dataguru.cn/thread-367824-1-1.html
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
     Language_.  Wadsworth & Brooks/Cole.

See Also:

     ‘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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值