假设我有2个矩阵
A <-matrix(runif(100), ncol=10)
B <-matrix(runif(100), ncol=10)
如何找到所有索引(i,j),使得A [i,j]&lt; r1和B [i,j]> r2并将它们存储在矩阵C中,使得C [i,j] = 1,否则为0。这里r1,r2是介于0和1之间的两个数字。我是否必须将A和B转换为数据帧以提高效率?
A和B在我原来的问题中非常大,因此效率是第一优先。
答案 0 :(得分:1)
matrix
操作可能比转换为data.frame
r1 <- 0.3
r2 <- 0.7
C <- A < r1 & B > r2
C[] <- as.numeric(C)
C
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 0 0 0 0 0 0 0 0 0 1
# [2,] 0 0 0 0 0 0 1 0 0 0
# [3,] 0 0 0 0 0 0 0 1 0 0
# [4,] 0 0 0 0 0 0 0 1 0 0
# [5,] 0 0 0 0 0 0 0 1 0 1
# [6,] 1 0 0 0 0 0 0 0 1 0
# [7,] 0 0 0 0 1 0 0 0 0 0
# [8,] 0 0 0 0 0 0 0 0 0 0
# [9,] 0 0 0 1 0 1 0 1 0 0
#[10,] 0 0 0 0 0 0 0 0 0 0
答案 1 :(得分:1)
我认为基本矩阵数学可能更快(实际上对于n = 10e6,其超出6倍)
set.seed(10)
A <-matrix(runif(1000000), ncol=1000)
B <-matrix(runif(1000000), ncol=1000)
r1<-0.5
r2<-0.3
C<-(A > r1) * (B < r2)
TIMING
# timing
A <-matrix(runif(1000000), ncol=1000)
B <-matrix(runif(1000000), ncol=1000)
> system.time({C <- A < r1 & B > r2
+ C[] <- as.numeric(C)
+ })
user system elapsed
0.06 0.00 0.06
>
> system.time(C<-(A > r1) * (B < r2))
user system elapsed
0.02 0.00 0.01