Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: All combinations of binary matrix

I am looking for a simple way to get all combinations of an binary matrix. I tried already the function perms() but did not get a proper result.

I have for example an matrix N x N filled up with 1 and -1. With N=2 there would be 2^4 possible combinations of 1 and -1 like

       (1 1)          (1  1)          (-1 -1)
M(1) = (1 1) , M(2) = (1 -1) , M(3) = ( 1  1) and so on...

When I use perms() I don't get for example the first matrix.

How can I fix that?

like image 993
Gilfoyle Avatar asked Oct 18 '22 18:10

Gilfoyle


1 Answers

You can represent all numbers between 0 and 2^(N^2)-1 as binary numbers, and then reshape:

N = 2;
v = (1:2^(N^2))-1;
A = dec2bin(v)' - '0'; %'// Or use: decimalToBinaryVector(v)';
A(A==0) = -1;
A = reshape(A,N,N,2^(N^2));
like image 169
Itamar Katz Avatar answered Oct 21 '22 14:10

Itamar Katz