矩阵NxM如何与Nx1 1xM进行R乘法? [英] How to do R multiplication with Nx1 1xM for Matrix NxM?

查看:322
本文介绍了矩阵NxM如何与Nx1 1xM进行R乘法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个简单的列(Nx1)乘以行(1xM)乘法,得到(NxM)矩阵. 我按顺序创建一行并通过转置类似序列创建列的代码

I want to do a simple column (Nx1) times row (1xM) multiplication, resulting in (NxM) matrix. Code where I create a row by sequence, and column by transposing a similar sequence

row1 <- seq(1:6) 
col1 <- t(seq(1:6))      
col1 * row1

输出表明R认为矩阵更像列

Output which indicates that R thinks matrices more like columns

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    4    9   16   25   36

预期输出:NxM矩阵.

Expected output: NxM matrix.

操作系统:Debian 8.5
Linux内核:4.6反向端口
硬件:华硕Zenbook UX303UA

OS: Debian 8.5
Linux kernel: 4.6 backports
Hardware: Asus Zenbook UX303UA

推荐答案

在这种情况下,使用outer是更自然的选择

In this case using outer would be a more natural choice

outer(1:6, 1:6)

通常对于两个数字矢量 xy,矩阵秩为1的运算可以计算为

In general for two numerical vectors x and y, the matrix rank-1 operation can be computed as

outer(x, y)

如果要使用实数矩阵乘法例程,请使用tcrossprod:

If you want to resort to real matrix multiplication routines, use tcrossprod:

tcrossprod(x, y)

如果xy中的任何一个都是具有维度的矩阵,请使用as.numeric首先将其转换为向量.

If either of your x and y is a matrix with dimension, use as.numeric to cast it as a vector first.

不建议为此使用常规矩阵乘法运算"%*%".但是,如果需要,请确保获得合适的尺寸:x是一个单列矩阵,而y是一个单行矩阵,所以x %*% y.

It is not recommended to use general matrix multiplication operation "%*%" for this. But if you want, make sure you get comformable dimension: x is a one-column matrix and y is a one-row matrix, so x %*% y.

您能谈谈效率吗?

Can you say anything about efficiency?

已知矩阵等级1操作受内存限制.因此,请确保我们使用gc()进行垃圾回收,以告诉R在每次复制后从堆中释放内存(否则您的系统将停滞):

Matrix rank-1 operation is known to be memory-bound. So make sure we use gc() for garbage collection to tell R to release memory from heap after every replicate (otherwise your system will stall):

x <- runif(500)
y <- runif(500)
xx <- matrix(x, ncol = 1)
yy <- matrix(y, nrow = 1)

system.time(replicate(200, {outer(x,y); gc();}))
#   user  system elapsed 
#  4.484   0.324   4.837 

system.time(replicate(200, {tcrossprod(x,y); gc();}))
#   user  system elapsed 
#  4.320   0.324   4.653 

system.time(replicate(200, {xx %*% yy; gc();}))
#   user  system elapsed 
#  4.372   0.324   4.708 

在性能方面,它们非常相似.

In terms of performance, they are all very alike.

跟进

当我回来时,我看到了另一个答案,它带有不同的基准.好吧,这取决于问题的大小.如果仅尝试一个小示例,就无法消除所有三个函数的函数解释/调用开销.如果你这样做

When I came back I saw another answer with a different benchmark. Well, the thing is, it depends on the problem size. If you just try a small example you can not eliminate function interpretation / calling overhead for all three functions. If you do

x <- y <- runif(500)
microbenchmark(tcrossprod(x,y), x %*% t(y), outer(x,y), times = 200)

您将再次看到大致相同的性能.

you will see roughly identical performance again.

#Unit: milliseconds
#             expr     min      lq     mean  median      uq      max neval cld
# tcrossprod(x, y) 2.09644 2.42466 3.402483 2.60424 3.94238 35.52176   200   a
#       x %*% t(y) 2.22520 2.55678 3.707261 2.66722 4.05046 37.11660   200   a
#      outer(x, y) 2.08496 2.55424 3.695660 2.69512 4.08938 35.41044   200   a

这篇关于矩阵NxM如何与Nx1 1xM进行R乘法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆