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

后端 未结 4 1131
南笙
南笙 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:05

    we can also use the 'combvec' instruction in matlab

        no_inp=3 % number of inputs we want...in this case we have 3 inputs                  
        a=[1 2 3]
        b=[1 2 3]
        c=[1 2 3]
    
        pre_final=combvec(c,b,a)';
        final=zeros(size(pre_final));
    
        for i=1:no_inp
        final(:,i)=pre_final(:,no_inp-i+1);
        end
        final 
    

    Hope it helps. Good luck.

提交回复
热议问题