我必须设置A={1,2,3}和B={a,b,c,d,e}。我想要来自A和B的元素子集的集合,它们至少包含A的一个元素和B的一个元素。正如这里所明确的:Cartesian product with all elements,我需要使用以下公式:
P(A ∪ B)∖(P(A) ∪ P(B))在R中,我尝试了以下查询:
require(HapEstXXR)
A <- c(1,2,3)
B <- c("A", "B", "C", "D", "E")
setdiff(powerset(union(A,B)), union(powerset(A),powerset(B)))结果我得到了221个元素。据我所知,应该有(2^3-1)(2^5-1) = 217元素。
我的查询是错误的吗?
发布于 2015-07-02 21:32:32
length( s1 <- powerset(union(A,B)) ) #255
length( s2 <- union(powerset(A),powerset(B)) ) # 38255-38 = 217,这似乎就是你要找的
但是,并非所有s2都包含在s1中
setdiff(s2, s1)
# 8 sets to create.
# 32 sets to create.
# 256 sets to create.
# [[1]]
# [1] 1 2
#
# [[2]]
# [1] 1 3
#
# [[3]]
# [1] 2 3
#
# [[4]]
# [1] 1 2 3这4个元素解释了221和217之间的差异。
发布于 2015-07-02 22:29:33
不要混用类:
length( z <- setdiff(powerset(union(A,B)), union(powerset(as.character(A)),powerset(B))) )
# 217请注意,as.character已应用于A。
https://stackoverflow.com/questions/31185513
复制相似问题