R中反向下三角矩阵的树列表
Tree list to reverse-lower triangular matrix in R
我将如何转换
m = list(1,2:3,4:6,7:10)
到
[,1] [,2] [,3] [,4]
[1,] 0 0 0 10
[2,] 0 0 6 9
[3,] 0 3 5 8
[4,] 1 2 4 7
非常感谢您的想法或指导!感谢您的耐心等待,以防问题太幼稚或需要额外信息(我很乐意提供)。
我将向前抛出一个基本的 R 方法
# Create matrix with dimensions defined by the length of your list
mat <- matrix(0, length(m), length(m))
# Fill in desired order
mat[upper.tri(mat, TRUE)] <- unlist(m)
# Order rows
mat[length(m):1, ]
1) 在 lapply
下面向 m
的每个组件附加 n
个零,sapply
取第一个n
每个组件的元素 m
将结果重塑为矩阵。最后我们反转结果矩阵的行的顺序。即使 m
没有定义三角矩阵,这仍然有效:
n <- length(m)
sapply(lapply(m, c, numeric(n)), head, n)[n:1, ]
给予:
[,1] [,2] [,3] [,4]
[1,] 0 0 0 10
[2,] 0 0 6 9
[3,] 0 3 5 8
[4,] 1 2 4 7
如果 n
可以为零,则使用 rev(seq_len(n))
代替 n:1
。
2) 直接 sapply
也可以。它在 m
的每个反向分量前加上适当数量的零,并重塑为矩阵:
sapply(m, function(v) c(numeric(n - length(v)), rev(v)))
这里有另一种选择可供考虑。这使用 lengths
计算出最长向量的长度,然后使用 vapply
,这将自动简化为矩阵(类似于 sapply
,但速度更快)。
len <- max(lengths(m)) ## What's the longest vector in m?
vapply(m, function(x) {
length(x) <- len ## Make all vectors the same length
rev(replace(x, is.na(x), 0)) ## Replace NA with 0 and reverse
}, numeric(len))
# [,1] [,2] [,3] [,4]
# [1,] 0 0 0 10
# [2,] 0 0 6 9
# [3,] 0 3 5 8
# [4,] 1 2 4 7
如果您使用的是稀疏矩阵(来自 Matrix
包),这些也适用:
> N <- lengths(m)
> sparseMatrix(i=1+length(m)-sequence(N), j=rep.int(N,N), x=unlist(m))
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . . 10
[2,] . . 6 9
[3,] . 3 5 8
[4,] 1 2 4 7
这与上三角矩阵的成语几乎相同:
> sparseMatrix(i=sequence(N), j=rep.int(N,N), x=unlist(m))
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] 1 2 4 7
[2,] . 3 5 8
[3,] . . 6 9
[4,] . . . 10
我将如何转换
m = list(1,2:3,4:6,7:10)
到
[,1] [,2] [,3] [,4]
[1,] 0 0 0 10
[2,] 0 0 6 9
[3,] 0 3 5 8
[4,] 1 2 4 7
非常感谢您的想法或指导!感谢您的耐心等待,以防问题太幼稚或需要额外信息(我很乐意提供)。
我将向前抛出一个基本的 R 方法
# Create matrix with dimensions defined by the length of your list
mat <- matrix(0, length(m), length(m))
# Fill in desired order
mat[upper.tri(mat, TRUE)] <- unlist(m)
# Order rows
mat[length(m):1, ]
1) 在 lapply
下面向 m
的每个组件附加 n
个零,sapply
取第一个n
每个组件的元素 m
将结果重塑为矩阵。最后我们反转结果矩阵的行的顺序。即使 m
没有定义三角矩阵,这仍然有效:
n <- length(m)
sapply(lapply(m, c, numeric(n)), head, n)[n:1, ]
给予:
[,1] [,2] [,3] [,4]
[1,] 0 0 0 10
[2,] 0 0 6 9
[3,] 0 3 5 8
[4,] 1 2 4 7
如果 n
可以为零,则使用 rev(seq_len(n))
代替 n:1
。
2) 直接 sapply
也可以。它在 m
的每个反向分量前加上适当数量的零,并重塑为矩阵:
sapply(m, function(v) c(numeric(n - length(v)), rev(v)))
这里有另一种选择可供考虑。这使用 lengths
计算出最长向量的长度,然后使用 vapply
,这将自动简化为矩阵(类似于 sapply
,但速度更快)。
len <- max(lengths(m)) ## What's the longest vector in m?
vapply(m, function(x) {
length(x) <- len ## Make all vectors the same length
rev(replace(x, is.na(x), 0)) ## Replace NA with 0 and reverse
}, numeric(len))
# [,1] [,2] [,3] [,4]
# [1,] 0 0 0 10
# [2,] 0 0 6 9
# [3,] 0 3 5 8
# [4,] 1 2 4 7
如果您使用的是稀疏矩阵(来自 Matrix
包),这些也适用:
> N <- lengths(m)
> sparseMatrix(i=1+length(m)-sequence(N), j=rep.int(N,N), x=unlist(m))
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] . . . 10
[2,] . . 6 9
[3,] . 3 5 8
[4,] 1 2 4 7
这与上三角矩阵的成语几乎相同:
> sparseMatrix(i=sequence(N), j=rep.int(N,N), x=unlist(m))
4 x 4 sparse Matrix of class "dgCMatrix"
[1,] 1 2 4 7
[2,] . 3 5 8
[3,] . . 6 9
[4,] . . . 10