使用 R 创建整数相似度矩阵
我有一个对角线等于 0 且非对角线都等于 1(单位矩阵的逆)的矩阵:
mat1 <- matrix(c(0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0), 5, 5)
我还有一个向量,其长度始终与矩阵的暗角相同,并且始终从零开始:
vec1 <- c(0,1,2,3,4)
使用对于这两个对象,我想创建一个如下所示的矩阵:
mat2 <- matrix(c(0,1,2,3,4,1,0,1,2,3,2,1,0,1,2,3,2,1,0,1,4,3,2,1,0), 5, 5)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 2 3 4
[2,] 1 0 1 2 3
[3,] 2 1 0 1 2
[4,] 3 2 1 0 1
[5,] 4 3 2 1 0
我想要一个能够泛化的操作,这样,如果我有一个 9 × 9 的矩阵,以及一个 0:8 的向量,我就可以获得等效的结果。关于如何解决这个问题有什么想法吗?
I have a matrix with diagonals equal to zero and off-diagonals all equal to one (the inverse of an identity matrix):
mat1 <- matrix(c(0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0), 5, 5)
I also have a vector that is always the same length as the dims of the matrix and always starts at zero:
vec1 <- c(0,1,2,3,4)
using these two objects I want to create a matrix that looks like this:
mat2 <- matrix(c(0,1,2,3,4,1,0,1,2,3,2,1,0,1,2,3,2,1,0,1,4,3,2,1,0), 5, 5)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 2 3 4
[2,] 1 0 1 2 3
[3,] 2 1 0 1 2
[4,] 3 2 1 0 1
[5,] 4 3 2 1 0
I want an operation that will generalize so that if I have a matrix of dims 9 by 9, for example, and a vector of 0:8 I can achieve the equivalent result. Any ideas for how to approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 vec1 以零开头,那么您可以这样做:
因此无需在输入中采用 mat1,因为该值实际上是多余的。您只需在函数内构造矩阵即可。
诀窍在于提供一系列 id 值以从向量中进行选择,然后将所有内容转换为矩阵。
编辑:如果您只想使用序列,您也可以这样做:
As vec1 starts with a zero, then you can do :
So there's no need to take the mat1 in the input, as that one is actually redundant. You can just construct the matrix within the function.
The trick is in providing a sequence of id values to select from the vector, and then transform everything to a matrix.
Edit : If you're only going to use sequences, you could as well do :
以下解决方案利用
upper.tri
和lower.tri
来隔离上三角矩阵和下三角矩阵。此外,它还利用sequence
来创建所需的向量序列。The following solution makes use of
upper.tri
andlower.tri
to isolate the upper and lower triangular matrix. In addition, it makes use ofsequence
to create the desired vector sequence.怎么样:
编辑
对于任意
vec1
:编辑2
事实上,没有必要先使用模数,然后再使用矩阵,abs 可以很好地使原始矩阵定义成为 1-liner:
所以,
并且
How about:
Edit
For arbitrary
vec1
:Edit 2
In fact, there's no need to use the modulus and then play with the matrix,
abs
will work fine to make the original matrix definition a 1-liner:So,
and