I have 4 bits combinations, and I want to change the size of the matrix HT depending on these combinations, the description below

2 views (last 30 days)
First ___ I creat the combinations like the follow:
n=4; %number of bits
r = [ 0:2^n-1 ];
A = [];
for i = 1 : n
A = [A;r];
end
c = [];
for i = 0 : n-1
c = [ 2^i c ];
end
c = c';
B = [];
for i = 1 : 2^n
B = [B c];
end
A=sign(bitand(A,B))'
Second_____
%A matrix has in each row 4 bits : 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
HT=(n,3); % the rows of HT is equal to the number of bits
When the bit combination is 0001 then HT will have one row (the last one) and When the bit combination is 0110 HT will have two rows (the second and the third one), also I think When the bit combination is 0000 it's better to put HT equal to zero;

Accepted Answer

DGM
DGM on 12 Jun 2021
Edited: DGM on 12 Jun 2021
Not sure how you want to handle HT, but here goes:
n = 4; %number of bits
% don't need all those loops
A = dec2bin(0:2^n-1)=='1' % should be in base toolbox
A = 16×4 logical array
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
% assuming HT already exists
HT = randi(9,n,3)
HT = 4×3
9 7 2 3 9 5 3 2 7 4 5 4
% idk how you want to handle a bunch of different-sized arrays
% i'm just going to throw them all in one cell array
HT_out = cell(size(A,1),1);
for r = 1:size(A,1)
thisHT = HT(A(r,:),:);
if isempty(thisHT)
HT_out{r} = [0 0 0];
else
HT_out{r} = thisHT;
end
end
format compact
celldisp(HT_out)
HT_out{1} = 0 0 0 HT_out{2} = 4 5 4 HT_out{3} = 3 2 7 HT_out{4} = 3 2 7 4 5 4 HT_out{5} = 3 9 5 HT_out{6} = 3 9 5 4 5 4 HT_out{7} = 3 9 5 3 2 7 HT_out{8} = 3 9 5 3 2 7 4 5 4 HT_out{9} = 9 7 2 HT_out{10} = 9 7 2 4 5 4 HT_out{11} = 9 7 2 3 2 7 HT_out{12} = 9 7 2 3 2 7 4 5 4 HT_out{13} = 9 7 2 3 9 5 HT_out{14} = 9 7 2 3 9 5 4 5 4 HT_out{15} = 9 7 2 3 9 5 3 2 7 HT_out{16} = 9 7 2 3 9 5 3 2 7 4 5 4
(scroll to see the rest of HT_out)

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!