can anybody help me?
Show older comments
function shannon_fano(h)
i=input('Enter the message ensembles :','s');
h=unique(sort(i));
j=histc(i,h);
e=fliplr(sort(j));
display (h);
display (e);
n=length(i);
m=e/n;
display (n);
display (m);
probabilities_calculation = zeros(size(j));
for i = 1:length(h) %find the probabilities of the symbols/occurence of each letter
probabilities_calculation(i) = sum(j==h(i))/length(h);
end
p = sort(probabilities_calculation(:),'descend');
% create the cell array of codes
codes = cell(size(h));
% call the recursive encoder function
codes = shannon_encoder(1,length(p),p,codes);
display (p);
function shannon_encoder(begin_point,end_point,p)
high_point = begin_point;
low_point = end_point;
high_sum = p(begin_point);
low_sum = p(end_point);
while(high_point ~= low_point-1)
if (high_sum > low_sum)
low_point = low_point - 1;
low_sum = low_sum + p(low_point);
else
high_point = high_point + 1;
high_sum = high_sum + p(high_point);
end
end
for i = begin_point:high_point
p(i) = 0;
end
for j = low_point:end_point
p(j) = 1;
end
if(high_point-begin_point+1 > 1)
shannon_encoder(begin_point,high_point,p);
end
if(end_point-low_point+1 > 1)
shannon_encoder(low_point,end_point,p);
end
7 Comments
KSSV
on 31 Jan 2020
don't use clear all inside the function. What is the problem? You have to specify the error.
John D'Errico
on 31 Jan 2020
Let me guess. You don't even use the variable keyword in the function, yet you pass it in?
The fact that you then immediately clear, makes the variable even more useless.
There is absolutely no need to use clear at the beginning of a function. In fact, it is the worst thing you can do, since you are clearing any arguments you passed into the fcuntino, before they could have been used.
If there is something else wrong, then only you can know, since you don't tell anypone what the code should do, or what it does that you think is wrong.
Arthur Richardo Samderubun
on 1 Feb 2020
Walter Roberson
on 1 Feb 2020
The line of code from the error message does not occur in the code you posted.
Arthur Richardo Samderubun
on 1 Feb 2020
Ioannis Andreou
on 1 Feb 2020
function shannon_encoder(begin_point,end_point,p)
In this definition of your function you allow 3 inputs but
codes = shannon_encoder(1,length(p),p,codes);
here you give 4 inputs. Thus you receive an error
Walter Roberson
on 1 Feb 2020
codes = shannon_encoder(1,length(p),p,codes);
That implies that the function accepts a codes parameter, but it does not.
The line also implies that the function returns a value, but it does not return anything.
When you have a function that does not return anything, it is only useful for its "side effects", or sometimes for debugging.
Is it correct that your function is intended to be recursive?
Accepted Answer
More Answers (0)
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!