在Matlab中生成包含给定集合的至少一个元素的所有组合 [英] Generating all combinations containing at least one element of a given set in Matlab

查看:91
本文介绍了在Matlab中生成包含给定集合的至少一个元素的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用combnk生成组合列表.如何生成组合的子集,该子集始终包含特定值.例如,对于combnk(1:10, 2),我只需要包含3和/或5的组合.是否有快速的方法?

I use combnk to generate a list of combinations. How can I generate a subset of combinations, which always includes particular values. For example, for combnk(1:10, 2) I only need combinations which contain 3 and/or 5. Is there a quick way to do this?

推荐答案

好,在您的特定示例中,从集合{1,...,10}中选择两个整数,以使所选整数之一为3或5会产生9 + 9-1 = 17个已知组合,因此您可以枚举它们.

Well, in your specific example, choosing two integers from the set {1, ..., 10} such that one of the chosen integers is 3 or 5 yields 9+9-1 = 17 known combinations, so you can just enumerate them.

通常,要从包含整数m的整数{1,...,n}中查找所有n-choose-k组合,这与查找(n-1)-choose-(k -1)整数{1,...,m-1,m + 1,...,n}的组合.

In general, to find all of the n-choose-k combinations from integers {1, ..., n} that contain integer m, that is the same as finding the (n-1)-choose-(k-1) combinations from integers {1, ..., m-1, m+1, ..., n}.

在matlab中,应该是

In matlab, that would be

combnk([1:m-1 m+1:n], k-1)

(即使m为1或n,该代码仍然有效.)

(This code is still valid even if m is 1 or n.)

这篇关于在Matlab中生成包含给定集合的至少一个元素的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆