Learn R Programming

simplextree (version 1.0.1)

nat_to_sub: nat_to_sub

Description

Computes the x^th (n choose 2) combination.

Usage

nat_to_sub(x, n, k)

Arguments

x

non-negative integers in the range c(1, choose(n, k))

n

numerator of the binomial coefficient

k

denominator of the binomial coefficient

Value

integer matrix whose columns give the combinadics of x.

Details

The mapping is done via an lexicographically-ordered combinadic mapping. In general, this function is not intended to be used to generate all (n choose k) combinations in the combinadic mapping.

References

McCaffrey, J. D. "Generating the mth lexicographical element of a mathematical combination." MSDN Library (2004).

Examples

Run this code
# NOT RUN {
library(simplextree)
all(nat_to_sub(seq(choose(100,2)), n = 100, k = 2) == combn(100,2))

## Generating pairwise combinadics is particularly fast
## Below: test to generate ~ 45k combinadics (note: better to use microbenchmark) 
system.time({
  x <- seq(choose(300,2))
  nat_to_sub(x, n = 300, k = 2L)
})

## Compare with generating raw combinations
system.time(combn(300,2))
# }

Run the code above in your browser using DataLab