一.问题描述
[1 1 1 0 1
0 1 1 0 1
0 0 1 1 0]
每两位3转换为一个十进制数,共5列,那么转换后是ceil(5/3)=2列。
[7 1
6 1
1 2]
二.问题分析
1.每八个字符划分为一个十进制,nwords=ceil(size(b,2)/8)
2.按列处理每行的字符,只不过1到8是一组,为第一列;9到17是二组,为第2列。
cb = zeros([size(b,1),nwords],'uint8')
for(j=1:size(b,2))
{
w = ceil(j/8);
//用bitset(A,bit,Value),bit没组都是1到8
cb(:,w) = bitset(cb(:,w),mod(j-1,8)+1,b(:,j))
}
三.实现
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cb = compactbit(b)
%
% b = bits array
% cb = compacted string of bits (using words of 'word' bits)
[nSamples nbits] = size(b);
nwords = ceil(nbits/8);%向上取整(正无穷方向)压缩bit->word.
cb = zeros([nSamples nwords], 'uint8');
for j = 1:nbits
w = ceil(j/8);
cb(:,w) = bitset(cb(:,w), mod(j-1,8)+1, b(:,j));
end