Generate all possible combinations of the elements of some vectors (Cartesian product)

后端 未结 4 1075
南笙
南笙 2020-11-22 13:32

I would like to generate all the possible combinations of the elements of a given number of vectors.

For example, for [1 2], [1 2] and

4条回答
  •  名媛妹妹
    2020-11-22 14:06

    Try ALLCOMB function at FileExchange.

    If you store you vectors in a cell array, you can run it like this:

    a = {[1 2], [1 2], [4 5]};
    allcomb(a{:})
    ans =
    
         1     1     4
         1     1     5
         1     2     4
         1     2     5
         2     1     4
         2     1     5
         2     2     4
         2     2     5
    

提交回复
热议问题