Info

This question is closed. Reopen it to edit or answer.

Matlab Scripts for Huffman Encoding of text files

1 view (last 30 days)
Tadele Gerem
Tadele Gerem on 25 Feb 2011
Closed: John D'Errico on 9 Jan 2023
Dear all, I need a Matlab script for the Huffman Encoding of text files not from a text input(file name input) but from (character,probability) input . here is the code.
clc; clear all; k=input('Enter the file name :','s'); fid = fopen(k,'r'); F = fread(fid); img = char(F'); mx=255; [x y z]=size(img); h(1:mx)=0; disp('Histogram building phase started....'); for i=1:y iy=img(i); val=double(iy); h(val)=h(val)+1; end end disp('Probability calculating phase started...'); i=1:mx p(i)=h(i)/(x*y); j=1; for i=1:mx if(p(i)~=0) lst(j)=i; lst(j+1)=p(i); j=j+2; end [tt,mx]=size(lst); disp('sorting phase started....'); for i=2:2:mx for j=i:2:mx if (lst(i)>lst(j)) temp1=lst(i-1); temp2=lst(i); lst(i-1)=lst(j-1); lst(i)=lst(j); lst(j-1)=temp1; lst(j)=temp2; end end end disp('Building Huffman Tree.....'); fhtree1(lst,img);
thanks
  3 Comments
Walter Roberson
Walter Roberson on 25 Feb 2011
Have you considered using sort() instead of your nested loop to sort the values ?

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!