MATLAB: All combinations of matrix elements

combinationmatrix elementspermutation

Hello,
I'd like to find all combinations of 2 elements in a matrix.
For example, a = [1:4; 5:8; 9:12]', the result should be [1*1 1*2 1*3 1*4 1*5 … 1*12 2*2 2*3 … 2*12 3*3 … 12*11 12*12]';
Thanks for your help!

Best Answer

Method One: matrix multiply and tril:
You can use a simple matrix multiply to do this:
>> z = a(:) * a(:)'
and use tril to get the triangular lower portion:
>> z(tril(true(size(z))))
Method Two: FEX submission combinator:
>> prod(a(combinator(numel(a),2,'c','r')),2)