我有两个向量,我想得到一个差异矩阵:
a = 1:4
b = 1:4
[,1] [,2] [,3] [,4]
[1,] 0 1 2 3
[2,] -1 0 1 2
[3,] -2 -1 0 1
[4,] -3 -2 -1 0
如何做到这一点?谢谢!
答案 0 :(得分:6)
-outer(1:4, 1:4, '-')
## [,1] [,2] [,3] [,4]
## [1,] 0 1 2 3
## [2,] -1 0 1 2
## [3,] -2 -1 0 1
## [4,] -3 -2 -1 0