gpt4 book ai didi

matlab - 如何在 MATLAB 中有效地编码位串?

转载 作者:行者123 更新时间:2023-12-04 18:19:27 25 4
gpt4 key购买 nike

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

bits = arithenco(z, counts);

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

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

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

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

最佳答案

与您的解决方案类似,但仅使用核心 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))

关于matlab - 如何在 MATLAB 中有效地编码位串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10992974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com