% Example 2.31 in the lecture note

pX = [0.5 0.25 0.125 0.125];            % pmf of X
SX = [1:length(pX)];                    % Source Alphabet
[dict,EL] = huffmandict(SX,pX);         % Create codebook

%% Pretty print the codebook.
codebook = dict;
for i = 1:length(codebook)
    codebook{i,2} = num2str(codebook{i,2});
end
codebook

EL

%% Try to encode some random source string
n = 5; % Number of source symbols to be generated
sourceString = randsrc(1,10,[SX; pX]) % Create data using pX and SX
encodedString = huffmanenco(sourceString,dict) % Encode the data