Fast computation of indices of all pairwise element of a vector of length n.

pairwise_combination_indices(n, self = FALSE)

Arguments

n

A number giving the number of elements to create all pairwise indices from

self

A logical that determines whether a column should also be multiplied by itself.

Value

A matrix with n*(n+1)/2 rows (if self=TRUE) or n*(n-1)/2 rows (if self=FALSE, the default) and two columns gicing all possible combinations of indices.

Details

Note that the output order of columns corresponds to the order of the columns in x. First column 1 is multiplied with each of the other columns, then column 2 with the remaining columns etc.

Author

Claus Ekstrom <claus@rprimer.dk>

Examples


pairwise_combination_indices(3)
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    1    3
#> [3,]    2    3
pairwise_combination_indices(4, self=TRUE)
#>       [,1] [,2]
#>  [1,]    1    1
#>  [2,]    1    2
#>  [3,]    1    3
#>  [4,]    1    4
#>  [5,]    2    2
#>  [6,]    2    3
#>  [7,]    2    4
#>  [8,]    3    3
#>  [9,]    3    4
#> [10,]    4    4