java 矩阵每行每列取一个数和最小值,从矩阵中每一列获取最小值的最快方法?...

这篇博客探讨了在不同形状的矩阵(如方阵、宽矩阵和高矩阵)中提取每列最小值的最有效方法。通过使用`pmin`函数在矩阵的行上操作,博主进行了基准测试,比较了不同矩阵类型下的性能。结果表明,对于方阵和宽矩阵,`pmin`在行上的应用表现更优。
摘要由CSDN通过智能技术生成

What is the fastest way to extract the min from each column in a matrix?

EDIT:

Moved all the benchmarks to the answer below.

Using a Tall, Short or Wide Matrix:

## TEST DATA

set.seed(1)

matrix.inputs

"Square Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=400), # 400 x 400

"Tall Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), nrow=4000), # 4000 x 40

"Wide-short Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=4000), # 40 x 4000

"Wide-tall Matrix" = matrix(sample(seq(1e6), 4^2*1e5, T), ncol=4000), # 400 x 4000

"Tiny Sq Matrix" = matrix(sample(seq(1e6), 4^2*1e2, T), ncol=40) # 40 x 40

)

解决方案

Here is one that is faster on square and wide matrices. It uses pmin on the rows of the matrix. (If you know a faster way of splitting the matrix into its rows, please feel free to edit)

do.call(pmin, lapply(1:nrow(mat), function(i)mat[i,]))

Using the same benchmark as @RicardoSaporta:

$`Square Matrix`

test elapsed relative

3 pmin.on.rows 1.370 1.000

1 apl 1.455 1.062

2 cmin 2.075 1.515

$`Wide Matrix`

test elapsed relative

3 pmin.on.rows 0.926 1.000

2 cmin 2.302 2.486

1 apl 5.058 5.462

$`Tall Matrix`

test elapsed relative

1 apl 1.175 1.000

2 cmin 2.126 1.809

3 pmin.on.rows 5.813 4.947

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值