LaTeXFan
LaTeXFan

Reputation: 1231

Choose k distinct and increasing index from n

Consider the formula in the following picture, where j and n are fixed integers. It is clear that n cannot be large. I am considering using n=10. The key feature is to find all distinct j-tubes (i_1 < ... < i_j) out of n. Is there an easy way to achieve this in R, please? Could anyone help me, please? photo

Upvotes: 0

Views: 117

Answers (1)

bgoldst
bgoldst

Reputation: 35324

This is just combinations, selecting j from n without regard to order. Of course, the mapping of the elements of the combination to i1ij will require sorting them (in order to satisfy the constraint i1 < … < ij), but this is actually how combn() returns its results by default:

jtubes <- function(j,n) combn(n,j);
jtubes(3,5);
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    1    1    1    1    1    1    2    2    2     3
## [2,]    2    2    2    3    3    4    3    3    4     4
## [3,]    3    4    5    4    5    5    4    5    5     5

combn() returns the result as a matrix with one combination per column, but you can change that to per row with a single call to t().

Upvotes: 3

Related Questions