MATLAB: 4 matrix generation depending on a specific value,

binarycombinationsmatrix

i want to generate a matrix of all possible binary combinations dpeending on a specfic 1×4 vector, and the vector elements are randomly generated from 1:4.
for example if the vector is [2 3 2 4], the first matrix should be all possible combinations of 2 bits
[0 0; 0 1; 1 0 ; 11]
, the second matrix should be all possbile combinations of 3 bits,
[ 0 0 0; 0 0 1; 0 1 0; 0 1 1; 1 0 0; 1 0 1; 1 1 0 ; 1 1 1]
and so on..

Best Answer

Try this
v = [2 3 2 4];
C = arrayfun(@(x) {dec2bin(0:2^x-1)-48}, v);
Result
>> C{1} % first matrix
ans =
0 0
0 1
1 0
1 1
>> C{2} % second matrix
ans =
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
>> C{3} % third matrix
ans =
0 0
0 1
1 0
1 1
>> C{4} % fourth matrix
ans =
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1