如何更高效地创建这样的矩阵?

huangapple go评论71阅读模式
英文:

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 &lt;- 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
英文:
&gt; 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 &lt;- seq(10), you can try dist

&gt; 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
&gt; 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

&gt; 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

使用 matrixlower.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 &lt;- function(Cols){
Dist &lt;- matrix(0:Cols, nrow=Cols+1, ncol=Cols, byrow=T)[-(Cols+1),]
Dist[lower.tri(Dist)] &lt;- 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

huangapple
  • 本文由 发表于 2023年5月24日 21:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定