Octave - 霍夫曼代码不起作用 - SIG 的所有元素必须是 [1,N] 范围内的整数 [英] Octave - Huffman code doesn't work - All elements of SIG must be integers in the range [1,N]
问题描述
我在 Octave 中使用 huffmandict 和 huffmanenco 时遇到问题.
这是我的错误:
<块引用>error: huffmanenco: SIG 的所有元素必须是范围内的整数[1,N]
这是我的代码:
inputSig = [1 1 2 6 6 6 6 4 5 5];list_symb = [1 2 6 4 5];list_proba = [0.2, 0.1, 0.4, 0.1, 0.2];dict = huffmandict(list_symb,list_proba);代码 = huffmanenco(inputSig,dict);
我的字典是
dict ={[1,1] = 1[1,2] = 0 1[1,3] = 0 0 1[1,4] = 0 0 0 0[1,5] = 0 0 0 1}
所以我的错误在于这条线
code = huffmanenco(inputSig,dict);
因为我的 dict 的长度是 5,而我的 inputSig 的长度是 10.
如何在没有此错误的情况下进行霍夫曼编码?
但是,此代码似乎适用于 Matlab.
你说
<块引用>因为我的 dict 的长度是 5,而我的 inputSig 的长度是 10.
这不是您收到此错误的原因.来自文档:
<块引用>一个限制是信号集必须严格属于范围 '[1,N]' 且 'N = length (dict)'.
换句话说,您的dict"仅包含 5 个单元格,但您的inputSig"包含 [1,6] 而不是 [1,5] 范围内的整数.
因此,您基本上必须在 [1,5] 范围内重新编码"/映射您的信号(即范围 [1,5] 将成为索引/标签,到您的实际符号数组).>
例如
inputSig = [1 1 2 6 6 6 6 4 5 5];list_symb = unique( inputSig );list_proba = [0.2, 0.1, 0.4, 0.1, 0.2];dict = huffmandict( list_symb, list_proba );[~, idx] = ismember( inputSig, list_symb );代码 = huffmanenco( idx, dict )% 代码 = 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0
<小时>
附注.为了完成,下一个显而易见的问题是,鉴于整个实际交易品种与指数"业务,您如何对此进行解码.简单的;您使用解码输出(对应于索引)并将其用作 list_symb 向量的索引向量,从而检索原始符号.即:
deco = huffmandeco ( code, dict )% 装饰 = 1 1 2 5 5 5 5 3 4 4list_symb( 装饰 )% ans = 1 1 2 6 6 6 6 4 5 5
I have a problem in Octave using huffmandict and huffmanenco.
Here is my error :
error: huffmanenco: all elements of SIG must be integers in the range [1,N]
Here is my code :
inputSig = [1 1 2 6 6 6 6 4 5 5];
list_symb = [1 2 6 4 5];
list_proba = [0.2, 0.1, 0.4, 0.1, 0.2];
dict = huffmandict(list_symb,list_proba);
code = huffmanenco(inputSig,dict);
my dict is
dict =
{
[1,1] = 1
[1,2] = 0 1
[1,3] = 0 0 1
[1,4] = 0 0 0 0
[1,5] = 0 0 0 1
}
So my error is with the line
code = huffmanenco(inputSig,dict);
because the lenght of my dict is 5 and my lenght of my inputSig is 10.
How can I do my huffman coding without this error?
However, this code seems to work on Matlab.
You say
because the length of my dict is 5 and the length of my inputSig is 10.
That's not quite why you get this error. From the documentation:
A restriction is that a signal set must strictly belong in the range '[1,N]' with 'N = length (dict)'.
In other words, your 'dict' only contains 5 cells, but your 'inputSig' contains integers in the range [1,6] instead of [1,5].
Therefore, you basically have to 'recode' / map your signal in the range [1,5] (i.e. the range [1,5] will become indices/labels, to an array of your actual symbols).
E.g.
inputSig = [1 1 2 6 6 6 6 4 5 5];
list_symb = unique( inputSig );
list_proba = [0.2, 0.1, 0.4, 0.1, 0.2];
dict = huffmandict( list_symb, list_proba );
[~, idx] = ismember( inputSig, list_symb );
code = huffmanenco( idx, dict )
% code = 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0
PS. For completion, the obvious next question is how do you decode this, given the whole 'actual symbol vs indices' business. Simple; you use the decoded output (which corresponds to indices) and use it as an index vector to the list_symb vector, thus retrieving the original symbols. I.e.:
deco = huffmandeco ( code, dict )
% deco = 1 1 2 5 5 5 5 3 4 4
list_symb( deco )
% ans = 1 1 2 6 6 6 6 4 5 5
这篇关于Octave - 霍夫曼代码不起作用 - SIG 的所有元素必须是 [1,N] 范围内的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!