MATLAB Examples

Create a Huffman Code Dictionary

This example shows how to create a Huffman code dictionary using the huffmandict function.

Create a vector of data symbols and assign a probability to each.

symbols = [1 2 3];
prob = [0.1 0.1 0.8];

Create a Huffman code dictionary. The most probable data symbol, 3, is associated with a one-digit codeword, while less probable data symbols are associated with two-digit codewords.

dict = huffmandict(symbols,prob)
dict =

  3x2 cell array

    {[1]}    {1x2 double}
    {[2]}    {1x2 double}
    {[3]}    {[       0]}

Display the second row of the dictionary. The output also shows that a Huffman encoder receiving the data symbol 2 substitutes the sequence 1 0.

ans =


ans =

     1     0