Given the sequence 1 2 3 4
, I would like to generate a matrix of pairs
1 2
2 3
3 4
to use as indexes for another matrix. What would be the fastest way to achieve this?
Given the sequence 1 2 3 4
, I would like to generate a matrix of pairs
1 2
2 3
3 4
to use as indexes for another matrix. What would be the fastest way to achieve this?
You could use embed()
, reversing the columns on the output.
embed(1:4, 2)[, 2:1]
# [,1] [,2]
# [1,] 1 2
# [2,] 2 3
# [3,] 3 4