Be the first to rate this file! 112 downloads (last 30 days) File Size: 3.03 KB File ID: #23080

combinations

by Gautam Vallabha

 

23 Feb 2009

Code covered by BSD License  

Returns combinations of a set of numeric or cell vectors (uses a memory-efficient algorithm)

Download Now | Watch this File

File Information
Description

C = COMBINATIONS(V1, V2, V3, ...Vn) returns the set of combinations formed by taking the first element from vector V1, the second element from vector V2, the third from vector V3, and so on.

    C is a KxN matrix, where K is the total
    number of combinations, and N is the number
    of (non-empty) input vectors.

    V1, V2, ... Vn can be either numeric or cell arrays.

C = COMBINATIONS(V1, V2, V3, ...Vn, CLASSNAME) specifies that all the numeric vectors have numeric type CLASSNAME (e.g., 'int8', 'single').

Examples:

c = combinations(1:5, 1:2)
c = combinations(1:5, 1:2, 'int8')
c = combinations(0:1,0:1,0:1,0:1,0:1,0:1,0:1,0:1,0:1,'int8');
 
c = combinations({'aa','bb'}, 1:3)
c = combinations({'aa','bb'}, {[1 2] [4 5 6]}, [true false])

----------------
MOTIVATION (in the spirit of reducing FileEx redundancy and clutter)

There are other FileExchange entries for calculating combinations:
  ALLCOMB (Filex# 10064)
  SETPROD (FileEx# 5898)
  CARTPROD (FileEx# 5475)

This implementation differs in two ways.

1) It allows combination of arbitrary cell arrays, and mixtures of cell and numeric arrays.

2) It use a memory-efficient algorithm that does not use intermediate matrices; furthermore, when CLASSNAME is specified, the specified numeric datatype is used consistently (there is no internal conversion from 'double'). This efficiency is relevant when the input consists of many small vectors or a few very large vectors.

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
07 Oct 2009 GERMÁN


NEED possible combinations of GROUPS OF 16 DIGITS [ a b c ! l t s 0 1 e o i]

09 Oct 2009 Gautam Vallabha

Germán, Your comment is rather cryptic. Can you elaborate? Also, search the FileExchange for "combinations nchoosek". Some of the existing entries, like COMBINATOR (#24325), may fit your requirement.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
permute Gautam Vallabha 23 Feb 2009 12:01:09
permutation Gautam Vallabha 23 Feb 2009 12:01:09
permutations Gautam Vallabha 23 Feb 2009 12:01:09
combination Gautam Vallabha 23 Feb 2009 12:01:09
combine Gautam Vallabha 23 Feb 2009 12:01:09
combinations Gautam Vallabha 23 Feb 2009 12:01:09
shuffle Gautam Vallabha 23 Feb 2009 12:01:09
cartesian product Gautam Vallabha 23 Feb 2009 12:01:09
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com