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?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With