gpt4 book ai didi

Octave - 霍夫曼代码不起作用 - SIG 的所有元素必须是 [1,N] 范围内的整数

转载 作者:行者123 更新时间:2023-12-03 21:42:08 25 4
gpt4 key购买 nike

我在使用 huffmandict 和 huffmanenco 时在 Octave 中遇到了问题。
这是我的错误:

error: huffmanenco: all elements of SIG must be integers in the range[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);
code = 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。

最佳答案

你说

because the length of my dict is 5 and the length of my inputSig is 10.


这不是您收到此错误的原因。从文档:

A restriction is that a signal set must strictly belong in the range '[1,N]' with '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 );
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

附注。为了完成,下一个显而易见的问题是,鉴于整个“实际交易品种与指数”业务,您如何对此进行解码。简单的;您使用解码后的输出(对应于索引)并将其用作 list_symb 向量的索引向量,从而检索原始符号。 IE。:
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] 范围内的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66929744/

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