英文:
How do you make a matrix like this more efficiently?
问题
这是翻译后的内容:
distances <- cbind(c(0,1,2,3,4,5,6,7,8,9),
c(1,0,1,2,3,4,5,6,7,8),
c(2,1,0,1,2,3,4,5,6,7),
c(3,2,1,0,1,2,3,4,5,6),
c(4,3,2,1,0,1,2,3,4,5),
c(5,4,3,2,1,0,1,2,3,4),
c(6,5,4,3,2,1,0,1,2,3),
c(7,6,5,4,3,2,1,0,1,2),
c(8,7,6,5,4,3,2,1,0,1),
c(9,8,7,6,5,4,3,2,1,0))
请注意,我已根据您的要求只返回翻译的代码部分。
英文:
distances <- cbind(c(0,1,2,3,4,5,6,7,8,9),
c(1,0,1,2,3,4,5,6,7,8),
c(2,1,0,1,2,3,4,5,6,7),
c(3,2,1,0,1,2,3,4,5,6),
c(4,3,2,1,0,1,2,3,4,5),
c(5,4,3,2,1,0,1,2,3,4),
c(6,5,4,3,2,1,0,1,2,3),
c(7,6,5,4,3,2,1,0,1,2),
c(8,7,6,5,4,3,2,1,0,1),
c(9,8,7,6,5,4,3,2,1,0))
Is there an easier way to make this matrix?
答案1
得分: 4
> toeplitz(0:9)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 2 3 4 5 6 7 8 9
[2,] 1 0 1 2 3 4 5 6 7 8
[3,] 2 1 0 1 2 3 4 5 6 7
[4,] 3 2 1 0 1 2 3 4 5 6
[5,] 4 3 2 1 0 1 2 3 4 5
[6,] 5 4 3 2 1 0 1 2 3 4
[7,] 6 5 4 3 2 1 0 1 2 3
[8,] 7 6 5 4 3 2 1 0 1 2
[9,] 8 7 6 5 4 3 2 1 0 1
[10,] 9 8 7 6 5 4 3 2 1 0
英文:
> toeplitz(0:9)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 2 3 4 5 6 7 8 9
[2,] 1 0 1 2 3 4 5 6 7 8
[3,] 2 1 0 1 2 3 4 5 6 7
[4,] 3 2 1 0 1 2 3 4 5 6
[5,] 4 3 2 1 0 1 2 3 4 5
[6,] 5 4 3 2 1 0 1 2 3 4
[7,] 6 5 4 3 2 1 0 1 2 3
[8,] 7 6 5 4 3 2 1 0 1 2
[9,] 8 7 6 5 4 3 2 1 0 1
[10,] 9 8 7 6 5 4 3 2 1 0
答案2
得分: 2
给定 v <- seq(10)
,您可以尝试使用 dist
函数:
> as.matrix(dist(v))
1 2 3 4 5 6 7 8 9 10
1 0 1 2 3 4 5 6 7 8 9
2 1 0 1 2 3 4 5 6 7 8
3 2 1 0 1 2 3 4 5 6 7
4 3 2 1 0 1 2 3 4 5 6
5 4 3 2 1 0 1 2 3 4 5
6 5 4 3 2 1 0 1 2 3 4
7 6 5 4 3 2 1 0 1 2 3
8 7 6 5 4 3 2 1 0 1 2
9 8 7 6 5 4 3 2 1 0 1
10 9 8 7 6 5 4 3 2 1 0
> dist(v, diag = TRUE, upper = TRUE)
1 2 3 4 5 6 7 8 9 10
1 0 1 2 3 4 5 6 7 8 9
2 1 0 1 2 3 4 5 6 7 8
3 2 1 0 1 2 3 4 5 6 7
4 3 2 1 0 1 2 3 4 5 6
5 4 3 2 1 0 1 2 3 4 5
6 5 4 3 2 1 0 1 2 3 4
7 6 5 4 3 2 1 0 1 2 3
8 7 6 5 4 3 2 1 0 1 2
9 8 7 6 5 4 3 2 1 0 1
10 9 8 7 6 5 4 3 2 1 0
或者使用 outer
函数和 abs
函数:
> abs(outer(v, v, `-`))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 2 3 4 5 6 7 8 9
[2,] 1 0 1 2 3 4 5 6 7 8
[3,] 2 1 0 1 2 3 4 5 6 7
[4,] 3 2 1 0 1 2 3 4 5 6
[5,] 4 3 2 1 0 1 2 3 4 5
[6,] 5 4 3 2 1 0 1 2 3 4
[7,] 6 5 4 3 2 1 0 1 2 3
[8,] 7 6 5 4 3 2 1 0 1 2
[9,] 8 7 6 5 4 3 2 1 0 1
[10,] 9 8 7 6 5 4 3 2 1 0
这些代码片段演示了如何计算向量 v
中元素之间的距离或差的绝对值。
英文:
Given v <- seq(10)
, you can try dist
> as.matrix(dist(v))
1 2 3 4 5 6 7 8 9 10
1 0 1 2 3 4 5 6 7 8 9
2 1 0 1 2 3 4 5 6 7 8
3 2 1 0 1 2 3 4 5 6 7
4 3 2 1 0 1 2 3 4 5 6
5 4 3 2 1 0 1 2 3 4 5
6 5 4 3 2 1 0 1 2 3 4
7 6 5 4 3 2 1 0 1 2 3
8 7 6 5 4 3 2 1 0 1 2
9 8 7 6 5 4 3 2 1 0 1
10 9 8 7 6 5 4 3 2 1 0
> dist(v, diag = TRUE, upper = TRUE)
1 2 3 4 5 6 7 8 9 10
1 0 1 2 3 4 5 6 7 8 9
2 1 0 1 2 3 4 5 6 7 8
3 2 1 0 1 2 3 4 5 6 7
4 3 2 1 0 1 2 3 4 5 6
5 4 3 2 1 0 1 2 3 4 5
6 5 4 3 2 1 0 1 2 3 4
7 6 5 4 3 2 1 0 1 2 3
8 7 6 5 4 3 2 1 0 1 2
9 8 7 6 5 4 3 2 1 0 1
10 9 8 7 6 5 4 3 2 1 0
or outer
+ abs
> abs(outer(v, v, `-`))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 2 3 4 5 6 7 8 9
[2,] 1 0 1 2 3 4 5 6 7 8
[3,] 2 1 0 1 2 3 4 5 6 7
[4,] 3 2 1 0 1 2 3 4 5 6
[5,] 4 3 2 1 0 1 2 3 4 5
[6,] 5 4 3 2 1 0 1 2 3 4
[7,] 6 5 4 3 2 1 0 1 2 3
[8,] 7 6 5 4 3 2 1 0 1 2
[9,] 8 7 6 5 4 3 2 1 0 1
[10,] 9 8 7 6 5 4 3 2 1 0
答案3
得分: 1
使用 matrix
和 lower.tri
的“手动”方法
DoMat <- function(Cols){
Dist <- matrix(0:Cols, nrow=Cols+1, ncol=Cols, byrow=T)[-(Cols+1),]
Dist[lower.tri(Dist)] <- t(Dist)[lower.tri(t(Dist))]
Dist
}
DoMat(10)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 2 3 4 5 6 7 8 9
[2,] 1 0 1 2 3 4 5 6 7 8
[3,] 2 1 0 1 2 3 4 5 6 7
[4,] 3 2 1 0 1 2 3 4 5 6
[5,] 4 3 2 1 0 1 2 3 4 5
[6,] 5 4 3 2 1 0 1 2 3 4
[7,] 6 5 4 3 2 1 0 1 2 3
[8,] 7 6 5 4 3 2 1 0 1 2
[9,] 8 7 6 5 4 3 2 1 0 1
[10,] 9 8 7 6 5 4 3 2 1 0
英文:
A "manual" approach using matrix
and lower.tri
DoMat <- function(Cols){
Dist <- matrix(0:Cols, nrow=Cols+1, ncol=Cols, byrow=T)[-(Cols+1),]
Dist[lower.tri(Dist)] <- t(Dist)[lower.tri(t(Dist))]
Dist
}
DoMat(10)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 2 3 4 5 6 7 8 9
[2,] 1 0 1 2 3 4 5 6 7 8
[3,] 2 1 0 1 2 3 4 5 6 7
[4,] 3 2 1 0 1 2 3 4 5 6
[5,] 4 3 2 1 0 1 2 3 4 5
[6,] 5 4 3 2 1 0 1 2 3 4
[7,] 6 5 4 3 2 1 0 1 2 3
[8,] 7 6 5 4 3 2 1 0 1 2
[9,] 8 7 6 5 4 3 2 1 0 1
[10,] 9 8 7 6 5 4 3 2 1 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论