2
sign up log in tour help stack overflow careers × Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. Generating all combinations containing at least one element of a given set in Matlab I use to generate a list of combinations. How can I generate a subset of combinations, which always includes particular values. For example, for I only need combinations which contain 3 and/or 5. Is there a quick way to do this? combnk combnk(1:10, 2) matlab combinations edited Feb 24 at 11:27 knedlsepp 4,336 1 4 30 asked Oct 25 '10 at 11:24 Edward 296 2 5 11 1 If you aren't too limited by performance, you could just brute-force it, i.e. create a large number of combinations and then only selecting the good ones. Jonas Oct 25 '10 at 13:35 3 Answers 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. 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}. In matlab, that would be combnk([1:m‐1 m+1:n], k‐1) (This code is still valid even if is 1 or n.) m answered Oct 25 '10 at 14:50 Steve Tjoa 17.3k 6 42 74 Generating all combinations containing at least one element of a given se... http://stackoverflow.com/questions/4014071/generating-all-combinatio... 1 sur 2 15/04/2015 22:10

Decion Fusion

Embed Size (px)

DESCRIPTION

Générer des combinations

Citation preview

  • sign up log in tour help stack overflow careers

    Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.

    Generating all combinations containing at least one element of a given set in Matlab

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

    combnk

    combnk(1:10, 2)

    matlab combinations

    edited Feb 24 at 11:27knedlsepp4,336 1 4 30

    asked Oct 25 '10 at 11:24Edward296 2 5 11

    1

    If you aren't too limited by performance, you could just brute-force it, i.e. create a large number ofcombinations and then only selecting the good ones. Jonas Oct 25 '10 at 13:35

    3 Answers

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

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

    combnk([1:m1 m+1:n], k1)

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

    answered Oct 25 '10 at 14:50Steve Tjoa17.3k 6 42 74

    Generating all combinations containing at least one element of a given se... http://stackoverflow.com/questions/4014071/generating-all-combinatio...

    1 sur 2 15/04/2015 22:10

  • For a brute force solution, you can generate all your combinations with then use thefunctions and to find only those combinations that contain one or more of asubset of numbers. Here's how you can do it using your above example:

    COMBNKANY ISMEMBER

    v = 1:10; %# Set of elements

    vSub = [3 5]; %# Required elements (i.e. at least one must appear in the

    %# combinations that are generated)

    c = combnk(v,2); %# Find pairwise combinations of the numbers 1 through 10

    rowIndex = any(ismember(c,vSub),2); %# Get row indices where 3 and/or 5 appear

    c = c(rowIndex,:); %# Keep only combinations with 3 and/or 5

    EDIT:

    For a more elegant solution, it looks like and I had a similar idea. However, I'vegeneralized the solution so that it works for both an arbitrary number of required elements andfor repeated elements in . The function SUBCOMBNK will find all the combinations of values taken from a set that include at least one of the values in the set :

    Steve

    v k

    v vSub

    function c = subcombnk(v,vSub,k)

    %#SUBCOMBNK All combinations of the N elements in V taken K at a time and

    %# with one or more of the elements in VSUB as members.

    %# Errorchecking (minimal):

    if ~all(ismember(vSub,v))

    error('The values in vSub must also be in v.');

    end

    %# Initializations:

    index = ismember(v,vSub); %# Index of elements in v that are in vSub

    vSub = v(index); %# Get elements in v that are in vSub

    v = v(~index); %# Get elements in v that are not in vSub

    nSubset = numel(vSub); %# Number of elements in vSub

    nElements = numel(v); %# Number of elements in v

    c = []; %# Initialize combinations to empty

    Generating all combinations containing at least one element of a given se... http://stackoverflow.com/questions/4014071/generating-all-combinatio...

    2 sur 2 15/04/2015 22:10