3

For example, I have sets of text like so:

Column 1:

a
b

Column 2:

l
m
n

Column 3 :

v
w
x
y

And I want to combine them to get an output like this:

alv
alw
alx
aly
amv
amw
amx
amy
...

Which will output 24 combinations of text. If I were to only use the first two columns, it will output 2*3=6 combinations.

I'm not able to figure out how to do this in MATLAB. Any suggestions?

gnovice
  • 125,304
  • 15
  • 256
  • 359
playcode
  • 31
  • 2
  • Closely-related: [Matlab - Generate all possible combinations of the elements of some vectors](http://stackoverflow.com/q/4165859/52738), [MATLAB: Enumerating All Combinations of Items in An Arbitrary Number of Sets](http://stackoverflow.com/q/6607355/52738), [MATLAB: Combinations of an arbitrary number of cell arrays](http://stackoverflow.com/q/8492277/52738). – gnovice Dec 13 '11 at 17:35

1 Answers1

3

One solution is to use the function NDGRID to generate all of the combinations of indices into your sets:

C = {'ab' 'lmn' 'vwxy'};            %# Cell array of text sets
sizeVec = cellfun('prodofsize',C);  %# Vector of set sizes
[index3,index2,index1] = ndgrid(1:sizeVec(3),...  %# Create all the index
                                1:sizeVec(2),...  %#   combinations for
                                1:sizeVec(1));    %#   the sets
combMat = [C{1}(index1(:)); ...  %# Index each corresponding cell of C and
           C{2}(index2(:)); ...  %#   concatenate the results into one matrix
           C{3}(index3(:))].';

And you should get the following for combMat:

alv
alw
alx
aly
amv
amw
amx
amy
anv
anw
anx
any
blv
blw
blx
bly
bmv
bmw
bmx
bmy
bnv
bnw
bnx
bny

If you just want to get combinations for columns 1 and 2, remove the first input and output arguments from the call to NDGRID and remove C{3}(index3(:)) from the computation of combMat.

If you instead want C to be a cell array of cell arrays of strings instead of a cell array of character arrays, you can still use the exact same code above. The only difference will be that combMat will end up being a cell array of strings instead of a character array.

UPDATE:

I've actually created a generalized solution that can compute combinations for any number of sets (either character arrays or cell arrays of strings). You can find it in this answer to a closely-related question. To reproduce the above example, you would call it like so:

combMat = allcombs(C{:});
Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Thank you... Now, how about if my text not just stand from 1 character, I mean, the texts like follows column 1: One two three four column 2: car ball table column 3: play sit Does I still can to use the meshgrid function? – playcode Apr 12 '11 at 06:23
  • @user699844: I've updated my answer to address your new example. – gnovice Apr 12 '11 at 17:34
  • Thanks a lot for the help. What should I do if I want the output not as char, but as the cell array? Since as I use your code, I just get the char type. And I want the input like this : [MAT,idx] = meshgrid([PrefixCombination(1:5,1),PrefixCombination(1:5,2)]). Can I use this code? PrefixCombination are the cell arrays which the number of column can change as the change of the related variable. They contain one word in each cell and contain of many cells. And I want the output consist of the same number of column as the input.The number of rows are same for each columns.Thank you for the help.. – playcode Apr 13 '11 at 19:21
  • @playcode: I don't understand what you are asking. Could you explain it more clearly? – gnovice Apr 13 '11 at 20:08