R 按其包含的值排序的矩阵索引
我有这样一个矩阵:R 按其包含的值排序的矩阵索引,r,matrix,cell,R,Matrix,Cell,我有这样一个矩阵: mat<-matrix(c(10,45,2,15,3,98,1,7,13),nrow = 3) mat [,1] [,2] [,3] [1,] 10 15 1 [2,] 45 3 7 [3,] 2 98 13 有没有快速的方法 提前感谢您使用顺序作为索引,我们重新排列'mat'和cbind的行和col,以获得有序值的行/列索引 i1 <- order(mat) cbind(row(mat)[i1], co
mat<-matrix(c(10,45,2,15,3,98,1,7,13),nrow = 3)
mat
[,1] [,2] [,3]
[1,] 10 15 1
[2,] 45 3 7
[3,] 2 98 13
有没有快速的方法
提前感谢您使用
顺序
作为索引,我们重新排列'mat'和cbind的行
和col
,以获得有序值的行/列索引
i1 <- order(mat)
cbind(row(mat)[i1], col(mat)[i1])
# [,1] [,2]
#[1,] 1 3
#[2,] 3 1
#[3,] 2 2
#[4,] 2 3
#[5,] 1 1
#[6,] 3 3
#[7,] 1 2
#[8,] 2 1
#[9,] 3 2
i1您可以使用
arrayInd(order(mat), dim(mat), dimnames(mat))
# [,1] [,2]
# [1,] 1 3
# [2,] 3 1
# [3,] 2 2
# [4,] 2 3
# [5,] 1 1
# [6,] 3 3
# [7,] 1 2
# [8,] 2 1
# [9,] 3 2
非常感谢@lukeA!非常感谢@akrun!
arrayInd(order(mat), dim(mat), dimnames(mat))
# [,1] [,2]
# [1,] 1 3
# [2,] 3 1
# [3,] 2 2
# [4,] 2 3
# [5,] 1 1
# [6,] 3 3
# [7,] 1 2
# [8,] 2 1
# [9,] 3 2