2

我想在 MATLAB 中尽可能高效地将整数向量(uint8)存储为(空间)。到目前为止,我正在使用arithenco对向量进行编码:

bits = arithenco(z, counts);

好消息是它返回一个位向量。坏事是这些位以双精度形式存储。这意味着返回的向量大约是原始 uint8 向量的 64 倍,而整个想法是使事物更小。

那么是否有一种简单(且运行时高效)的方法来编码这些伪位,以便我真正获得空间改进?

我想出的唯一解决方案是使用bitset将所有这些位再次存储在 uint32 的向量中,但这似乎很麻烦而且速度不是很快,因为我必须遍历整个位向量明确地。

注意:我不能为此使用 Java API,否则这会相对容易。

4

3 回答 3

3

与您的解决方案类似,但仅使用核心 MATLAB 函数:

%# some random sequence of bits
bits = rand(123,1) > 0.5;

%# reshape each 8 bits as a column (with zero-padding if necessary)
numBits = numel(bits);
bits8 = false(8, ceil(numBits/8));
bits8(1:numBits) = bits(:);

%# convert each column to uint8
bits_packed = uint8( bin2dec(char(bits8'+'0')) );

比较尺寸:

>> whos bits bits_packed
  Name               Size            Bytes  Class      Attributes

  bits             123x1               123  logical              
  bits_packed       16x1                16  uint8          

解压/恢复原始位:

%# unpack
b = logical(dec2bin(bits_packed)' - '0');
b = b(:);

%# sanity check
isequal(bits, b(1:numBits))
于 2012-06-13T15:09:46.810 回答
1

经过一番搜索和尝试,我终于想出了这个解决方案:

bitCount = size(bits, 2);
bits8 = zeros(ceil(bitCount/8), 8);
bits8(1:bitCount) = bits;           % Reshape to (pseudo-)8-bit representation
comp = uint8(bi2de(bits8));         % Convert to efficient byte representation

这里的关键部分是bi2de函数,它“将二进制行向量 b 转换为非负十进制整数”。要再次获取这些位,可以使用de2bi函数。

于 2012-06-12T21:42:52.997 回答
0

您可以将它们转换为logical

     bitsLogical = logical(bits);

这在内存中应该更有效。但是你仍然会有转换步骤。因此,最好的办法是深入arithenco研究并将其更改为首先返回逻辑。


编辑 正如 OP 所说的那样,这不会被打包为位,而是作为字节。仍然是一个改进double

于 2012-06-12T08:29:35.973 回答