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)
Show older comments
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
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
% assuming HT already exists
HT = randi(9,n,3)
% 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)
(scroll to see the rest of HT_out)
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!