I have some problems in final part of Huffman encoding.
Currently I have my coding table in cell array
code =
{
[1,1] = 000
[1,2] = 001
[1,3] = 010
[1,4] = 011
[1,5] = 100
...
}
Where second index represent ascii character in my other cell array
huffman_tree =
{
[1,1] = A
[1,2] = B
[1,3] = C
[1,4] = D
[1,5] = E
...
}
I'm using following code for encoding input to output:
output= [];
for i=1:length(input)
x = findInArray(huffman_tree, input(i));
output= [output code(x)];
end
function [index] = findInArray(array, searched)
index = -1;
for i=1:length(array)
if array{i} == searched
index = i;
end
end
end
At this point my code is O(n^2) or even worse. I'm having problem with large input where
length(input) = 1000000
There must be some faster way to transform input with my coding table to output.