I am trying to get the binary key of each letter from my huffman dictionary. How can I do that? I tried pulling them directly from the cells using a for loop. Please help.

1 view (last 30 days)
function Project2(transmitter,Dictionary)
%probability vector goes in the dictionary
% symbols go in the transmitter
tf = ischar(transmitter)
if tf == 1
[dict,avglen] = huffmandict(num2cell(transmitter), Dictionary);
sig = char( randsrc(1,50,[double(transmitter); Dictionary]) );
comp = huffmanenco(sig,dict)
dsig = cell2mat(huffmandeco(comp,dict))
else
[dict, avglen] = huffmandict(transmitter,Dictionary);
sig = randsrc(100,1,[transmitter; Dictionary]);
comp = huffmanenco(sig,dict);
dsig = huffmandeco(comp,dict);
isequal(sig,dsig);
end
% b = length(transmitter)
% Binarykey = zeros(b,1)
% for k = 1:b
%
% Binarykey(k,1) = dict{k,2} + Binarykey(k,1) % how to find all binary keys
%
%
% end
dictionary.Symbols = transmitter
dictionary.Probabilities = Dictionary
dictionary.huffmandict = dict
dictionary.averagelength = avglen
dictionary.encoded = comp
dictionary.decoded = dsig
% dictionary.Binarykey = Binarykey
end

Answers (1)

Walter Roberson
Walter Roberson on 11 Oct 2019
The data type of the entries in the second column of the returned dictionary is always double.
In your commented out code you have
% Binarykey = zeros(b,1)
% for k = 1:b
%
% Binarykey(k,1) = dict{k,2} + Binarykey(k,1) % how to find all binary keys
so Binarykey would be initialized as double. dict{k,2} would be double, and Binarykey(k,1) would be double, so dict{k,2} + Binarykey(k,1) would give you a double result. But dict{k,2} will almost always be a vector rather than a scalar, so the right hand side will almost always be vector of double. Which you then try to store into the scalar double location Binarykey(k,1), and that is seldom going to fit.
You appear to be trying to create a single numeric array of the keys, even though the keys are of different lengths. In order to do that, the number of columns for each entry would have to be the same as the maximum length of a key. But if you try to implement that with zeros() then you will not be able to tell the difference between a key that ends with a 0, versus a shorter key that ends with a 1 followed by a padding of 0. You would have to take steps to ensure that the padding for each row was something that was neither 0 nor 1 so that you can tell padding apart from key.
Or perhaps at some point you slipped into thinking that the binary keys were string() objects ??

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!