在 R 中对大矩阵的每一行进行排序的最快方法 [英] Fastest way to sort each row of a large matrix in R

查看:26
本文介绍了在 R 中对大矩阵的每一行进行排序的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩阵:

set.seed(1)
a <- matrix(runif(9e+07),ncol=300)

我想对矩阵中的每一行进行排序:

I want to sort each row in the matrix:

> system.time(sorted <- t(apply(a,1,sort)))
   user  system elapsed 
  42.48    3.40   45.88 

我有很多 RAM 可以使用,但我想要一种更快的方式来执行此操作.

I have a lot of RAM to work with, but I would like a faster way to perform this operation.

推荐答案

嗯,我不知道在 R 中有很多方法可以更快地排序,问题是你只排序 300 个值,但是很多次.不过,您可以通过直接调用 sort.int 并使用 method='quick' 来获得一些额外的性能:

Well, I'm not aware of that many ways to sort faster in R, and the problem is that you're only sorting 300 values, but many times. Still, you can eek some extra performance out of sort by directly calling sort.int and using method='quick':

set.seed(1)
a <- matrix(runif(9e+07),ncol=300)

# Your original code
system.time(sorted <- t(apply(a,1,sort))) # 31 secs

# sort.int with method='quick'
system.time(sorted2 <- t(apply(a,1,sort.int, method='quick'))) # 27 secs

# using a for-loop is slightly faster than apply (and avoids transpose):
system.time({sorted3 <- a; for(i in seq_len(nrow(a))) sorted3[i,] <- sort.int(a[i,], method='quick') }) # 26 secs

但更好的方法应该是使用 parallel 包对矩阵的各个部分进行并行排序.然而,传输数据的开销似乎太大了,在我的机器上它开始交换,因为我只有"有 8 GB 内存:

But a better way should be to use the parallel package to sort parts of the matrix in parallel. However, the overhead of transferring data seems to be too big, and on my machine it starts swapping since I "only" have 8 GB memory:

library(parallel)
cl <- makeCluster(4)
system.time(sorted4 <- t(parApply(cl,a,1,sort.int, method='quick'))) # Forever...
stopCluster(cl)

这篇关于在 R 中对大矩阵的每一行进行排序的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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