generate a matrix from given Bit combinations

7 views (last 30 days)
Iam having a list of all possible bit combinations
For Example : if it is 3bit there will be (2)^3 - 1 combinations which is 7 combinations given below
A = [1,0,1 ; 1,1,1 ; 1,1,0 ; 0,1,1 ; 0,1,0 ; 0,0,1 ; 0,1,1]
Here aij used in below paragraph means ith row and jth column in B matrix
Now i want to generate a matrix based on visiting bit pattern for example for the above given bit combinations we need to generate a 7*7 matrix (because of 7 combinations), where first we will visit bit combination 1,0,1 and then 1,1,1 so cell a12 is 1 and other cells in 1st row is zero. In a12 - 1 represents 1,0,1 and 2 represents 1,1,1 ; which means if we visit 1,1,1 immediately after visiting 1,0,1 then a12 is 1 and remaining cells in that same row is zero because we dont want jumping from 1st combination(1,0,1) to (0,1,1) which is a14 is 0. so i need a matrix like given below
B = [ 0 1 0 0 0 0 0 ; 0 0 1 0 0 0 0 ; 0 0 0 1 0 0 0 ; 0 0 0 0 1 0 0 ; 0 0 0 0 0 1 0 ; 0 0 0 0 0 0 1 ; 0 0 0 0 0 0 0]
Here a12 is 1(since we first visit 1,0,1 and then 1,1,1) , a23 is 1 (we first visit 1,1,1 and then 1,1,0) similarly a34 is 1 , a45 is 1 , a56 is 1 , a67 is 1 and a78 is 1 (not possible so last row(7th) all zero row).
NOTE : i need a generalized code where i can generate a matrix based on bit combinations for example 4 bit means (2)^4 - 1 which is 15 combinations so 15*15 matrix with same logic above(a12 - 1,a23 - 1,a34 - 1 so on..). Thank you in advance and hope this explanation is understandable

Accepted Answer

Akira Agata
Akira Agata on 5 Jun 2023
Edited: Akira Agata on 5 Jun 2023
How about the following solution?
% Number of bits (-> please change this value)
Nbit = 3;
% Number of combination
Ncmb = 2^Nbit - 1;
% Create all the combinations of bits
c = dec2bin(1:Ncmb);
c = cellstr(c);
c = split(c, '');
c = c(:, 2:end-1);
A = str2double(c);
% Create B
B = triu(ones(Ncmb), 1) - triu(ones(Ncmb), 2);
% Show the result
disp(A)
0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
disp(B)
0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!