列出给定大小的向量的所有子集

时间:2021-12-25 21:46:43

The function choose(n,k) tells us how many subsets of size k exists for a set of n distinct elements. Suppose I need to actually list those subsets, how I create it? in other words I'm looking for a function that accepts a vector x (of length n) and a number k and returns a list of vectors, each of size k, with subsets of x. the length of the list should be, of course, choose(length(x),k). for example

函数choose(n,k)告诉我们对于一组n个不同元素存在多少个大小为k的子集。假设我需要实际列出这些子集,我是如何创建它的?换句话说,我正在寻找一个接受矢量x(长度为n)和数字k的函数,并返回一个矢量列表,每个矢量大小为k,子集为x。当然,列表的长度应该是(长度(x),k)。例如

enum.choose = function(x,k) {
    # implementation should go here
{

enum.choose(1:3,2)

# should be:
# [[1]]
#    1  2
# [[2]]
#    1  3
# [[3]]
#    2  3

1 个解决方案

#1


5  

EDIT

编辑


I realized that combn(1:3, 2, simplify = FALSE) gives you the list result you're looking for. If @Ramnath wishes to post an answer, this one will be deleted.

我意识到combn(1:3,2,simplify = FALSE)为你提供了你正在寻找的列表结果。如果@Ramnath希望发布答案,则会删除此答案。

> combn(1:3, 2, simplify = FALSE)
## [[1]]
## [1] 1 2

## [[2]]
## [1] 1 3

## [[3]]
## [1] 2 3

So using an *apply function on it will make the following function irrelevant.

因此,在其上使用* apply函数将使以下函数无关紧要。


Original

原版的


Making use of the comment from @Ramnath, your function might be something like this:

利用@Ramnath的评论,你的函数可能是这样的:

enum.choose <- function(x, k) {
  if(k > length(x)) stop('k > length(x)')
  if(choose(length(x), k)==1){
    list(as.vector(combn(x, k)))
  } else {
    cbn <- combn(x, k)
    lapply(seq(ncol(cbn)), function(i) cbn[,i])
  }
}

Test runs:

测试运行:

> enum.choose(1:3, 2)
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 1 3
# 
# [[3]]
# [1] 2 3
> enum.choose(c(1, 2, 5, 4), 3)
# [[1]]
# [1] 1 2 5
# 
# [[2]]
# [1] 1 2 4
# 
# [[3]]
# [1] 1 5 4
# 
# [[4]]
# [1] 2 5 4
> enum.choose(1:4, 4)
# [[1]]
# [1] 1 2 3 4
> enum.choose(1:5, 6)
# Error in enum.choose(1:5, 6) : k > length(x)

#1


5  

EDIT

编辑


I realized that combn(1:3, 2, simplify = FALSE) gives you the list result you're looking for. If @Ramnath wishes to post an answer, this one will be deleted.

我意识到combn(1:3,2,simplify = FALSE)为你提供了你正在寻找的列表结果。如果@Ramnath希望发布答案,则会删除此答案。

> combn(1:3, 2, simplify = FALSE)
## [[1]]
## [1] 1 2

## [[2]]
## [1] 1 3

## [[3]]
## [1] 2 3

So using an *apply function on it will make the following function irrelevant.

因此,在其上使用* apply函数将使以下函数无关紧要。


Original

原版的


Making use of the comment from @Ramnath, your function might be something like this:

利用@Ramnath的评论,你的函数可能是这样的:

enum.choose <- function(x, k) {
  if(k > length(x)) stop('k > length(x)')
  if(choose(length(x), k)==1){
    list(as.vector(combn(x, k)))
  } else {
    cbn <- combn(x, k)
    lapply(seq(ncol(cbn)), function(i) cbn[,i])
  }
}

Test runs:

测试运行:

> enum.choose(1:3, 2)
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 1 3
# 
# [[3]]
# [1] 2 3
> enum.choose(c(1, 2, 5, 4), 3)
# [[1]]
# [1] 1 2 5
# 
# [[2]]
# [1] 1 2 4
# 
# [[3]]
# [1] 1 5 4
# 
# [[4]]
# [1] 2 5 4
> enum.choose(1:4, 4)
# [[1]]
# [1] 1 2 3 4
> enum.choose(1:5, 6)
# Error in enum.choose(1:5, 6) : k > length(x)