1

我有两个向量,我想得到一个差异矩阵:

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

如何才能做到这一点?谢谢!

4

1 回答 1

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
于 2013-06-02T21:13:36.523 回答