0

Suppose we have two numbers [-1 1] and want to combine them in all combinations inside a matrix which has 6 columns . How can I compute all these 64 combinations of these numbers such that a result is a matrix of 64x6 dimension?

To make my question more clear, ending result would look something like this:

A = [-1 1 -1 1 -1 1; 1 -1 1 1 -1 -1 1; -1 1 -1 -1 1 -1; 1 -1 -1 1 -1 -1; etc.] with all 64 possible combinations. Since there are 64 combinations, a resulting matrix would have 64 rows and 6 columns since every row (vector) has 6 numbers.

Dario Mirić
  • 111
  • 3

1 Answers1

3

Note that your problem is equivalent to counting in binary, and replacing the zeros and ones with your desired values. You can use dec2bin to generate a matrix with one binary number per row, and then use that to index a vector of arbitrary values:

n = 6;                            % number samples per row
values = [-1, 1];                 % the two distinct values
len = 2^n;                        % total number of rows
binary = dec2bin(0:len-1, n)-'0'; % binary "counting" matrix
A = values(binary+1);             % indexing the values by the counting matrix
disp(A);

Try it online!

flawr
  • 10,814
  • 3
  • 41
  • 71