Replacing a for loop with a time efficient alternative ?

2 views (last 30 days)
So I am making my own algorithm to implement binary huffman encoding for grayscale images, my code works very well but the encoding process for loop takes too much time if the image has many levels, it goes like this:
(The S stands for symbols and the Levels is the input image reshaped into a vector, dict is my formed codeword dictionary which is similar to using huffmandict)
%Encoding the stream:
Encoded_Stream = [];
Decoding_Cell = {};
for i=1:length(Levels)
for j=1 : length(S)
if Levels(i) == dict{j,1}
Encoded_Stream = [Encoded_Stream dict{j,2}];
Decoding_Cell{i,1} = dict{j,2};
end
end
end
Is there any faster solution to find the corresponding codeword in the dictionary without using this second for loop
  9 Comments
Mahmoud Emad
Mahmoud Emad on 9 Apr 2020
Edited: Mahmoud Emad on 9 Apr 2020
I came up with an idea finally to encode the stream without using an array and a cell that change size each loop iteration. Thank you very much for your consideration it helped me come up with the idea :D

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!