为每个combin(n,k)组合获取排除的元素 [英] Getting the excluded elements for each of the combn(n,k) combinations
问题描述
假设我们生成了一个矩阵 A
,其中每一列包含 n
个元素的组合之一 k
。因此,其尺寸将为 k,choose(n,k)
。给出命令 combn(n,k)
产生这样的矩阵。我想得到的是另一个尺寸为(nk),choose(n,k)
的矩阵 B
每列 B [,j]
将包含已排除的 A ,j的
。 nk
元素]
Suppose we have generated a matrix A
where each column contains one of the combinations of n
elements in groups of k
. So, its dimensions will be k,choose(n,k)
. Such a matrix is produced giving the command combn(n,k)
. What I would like to get is another matrix B
with dimensions (n-k),choose(n,k)
, where each column B[,j]
will contain the excluded n-k
elements of A[,j]
.
以下是我使用获取表 B
的方式的示例。您认为这是一种安全的使用方法吗?还有另一种方法吗?
Here is an example of the way I use tho get table B
. Do you think it is a safe method to use? Is there another way?
n <- 5 ; k <- 3
(A <- combn(n,k))
(B <- combn(n,n-k)[,choose(n,k):1])
另一个示例
x<-c(0,1,0,2,0,1) ; k<- 4
(A <- combn(x,k))
(B <- combn(x,length(x)-k)[,choose(length(x),k):1])
该我以前的问题是该问题的一部分。
谢谢。
That previous question of mine is part of this problem.
Thank you.
推荐答案
使用Musa的向量中获得向量y的补语
B <- apply(A,2,function(z) x[is.na(pmatch(x,z))])
关于第一个示例:
B <- apply(A,2,function(z) (1:n)[is.na(pmatch((1:n),z))])
这篇关于为每个combin(n,k)组合获取排除的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!