# MATLAB Huffman Coding

Page 1 of 1

## 0 Replies - 789 Views - Last Post: 03 October 2013 - 01:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330666&amp;s=5e5df1c53ee08d1918efe029cd195e47&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 parkie02

Reputation: -1
• Posts: 24
• Joined: 15-May 13

# MATLAB Huffman Coding

Posted 03 October 2013 - 01:49 PM

Hallo, can somebody help me with my code. I am busy implementing the huffman code, but calculating the probabilities dont work right out. It has to take the two lowest probabilities and then add them together. Then it has to be sorted and done again until only two probabilities remain.
This is what my program do do
0.933522222222222 0.954933333333333 0.977122222222222
0.0228777777777778 0.0228777777777778 0.0228777777777778
0.0221888888888889 0.0221888888888889
0.0214111111111111

```format long;

%clearing all variableas and screen
clear all;
close all;
clc;

figure,imshow(a)

%converting an image to grayscale
I=a;

%size of the image
[f,g]=size(I);
Totalcount=f*g;

%variables using to find the probability
cnt=1;
sigma=0;

%computing the cumulative probability.
for i=0:f
k=I==i;
count(cnt)=sum(k(:)/>)

%pro array is having the probabilities
pro(cnt)=count(cnt)/Totalcount;
sigma=sigma+pro(cnt);
cumpro(cnt)=sigma;
cnt=cnt+1;
end;

%Symbols for an image
symbols = [0:f];

%Order probabilities
for q=1:f
matrix(1,q) = pro(q);
end;

sorted = sort(transpose(matrix),'descend');
%Sort probabilities and add them together
for n=2:f-1

for m=1:f-n

sorted(m,n) = sorted(m,n-1);
if m==(f-n)
sorted(f+1-n,n) = sorted(f+2-n,n-1)+sorted(f+1-n,n-1);
final = sort(sorted,'descend');
end

end

end

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }