java 矩阵乘向量_通过向量乘以矩阵行?

实际上, sweep 不是我电脑上最快的选择:

MyMatrix

MyVector

Rprof(tmp

t(t(MyMatrix) * MyVector) # first option

Rprof()

MyTimerTranspose=summaryRprof(tmp)$sampling.time

unlink(tmp)

Rprof(tmp

MyMatrix %*% diag(MyVector) # second option

Rprof()

MyTimerDiag=summaryRprof(tmp)$sampling.time

unlink(tmp)

Rprof(tmp

sweep(MyMatrix ,MARGIN=2,MyVector,`*`) # third option

Rprof()

MyTimerSweep=summaryRprof(tmp)$sampling.time

unlink(tmp)

Rprof(tmp

t(t(MyMatrix) * MyVector) # first option again, to check order

Rprof()

MyTimerTransposeAgain=summaryRprof(tmp)$sampling.time

unlink(tmp)

MyTimerTranspose

MyTimerDiag

MyTimerSweep

MyTimerTransposeAgain

这会产生:

> MyTimerTranspose

[1] 0.04

> MyTimerDiag

[1] 40.722

> MyTimerSweep

[1] 33.774

> MyTimerTransposeAgain

[1] 0.043

除了是最慢的选项之外,第二个选项达到内存限制(2046 MB) . 但是,考虑到剩下的选项,在我看来,双转置似乎比_966774好多了 .

Edit

只是重复尝试较小的数据:

MyMatrix

MyVector

n=100000

[...]

for(i in 1:n){

# your option

}

[...]

> MyTimerTranspose

[1] 5.383

> MyTimerDiag

[1] 6.404

> MyTimerSweep

[1] 12.843

> MyTimerTransposeAgain

[1] 5.428

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值