I have 4 bits combinations, and I want to change the size of the matrix HT depending on these combinations, the description below
1 次查看(过去 30 天)
显示 更早的评论
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;
采纳的回答
DGM
2021-6-12
编辑:DGM
2021-6-12
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!