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
KSSV on 31 Jan 2020
don't use clear all inside the function. What is the problem? You have to specify the error.
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.
??? Error using ==> Untitled22>shannon_encoder
Too many input arguments.
Error in ==> Untitled22 at 20
codes = shannon_encoder(1,length(p),p,codes);
this is the error
The line of code from the error message does not occur in the code you posted.
sorry im fix that now.
done.
can u help me now?
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
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?

Sign in to comment.

 Accepted Answer

Somebody can help me??
i want to ask, about shannon fano code
I already input character and get the results but entropy and coding efficency the result is NAN if i try to input the small number of characters all the results appear
this is the coding and you can try this coding, if somebody know that error i hope to inbox me on email ( arthur220895@gmail.com). thank you so much
clc
clear
format short g
keyword = input('INPUT KEYWORD : \n','s');
tic;
fprintf('\n %s \n\n\n',keyword);
symbols = sort(unique(keyword)) %,'descend'
probabilities_calculation = zeros(size(symbols))';
for i = 1:length(symbols) %find the probabilities of the symbols/occurence of each letter
probabilities_calculation(i,1) = sum(keyword== symbols(i))/length(keyword);
probabilities_calculation(i,2) = symbols(i);
probabilities_calculation(i,3) = sum(keyword== symbols(i));
end
rank = sortrows(probabilities_calculation);
new_rank = zeros(size(rank));
for i = 1:length(symbols) %find the probabilities of the symbols/occurence of each letter
fprintf('\n %f || %s\n',rank(length(symbols) - i+1,1),char(rank(length(symbols) - i+1,2)));
new_rank(i,:) = rank(length(symbols) - i+1,:);
end
new1 = sort(unique(new_rank(:,1)),'descend');
iterasi = 1;
for i=1:length(new1)
for j = 1:length(symbols)
if new1(i) == new_rank(length(symbols) - j+1,1)
data_baru(iterasi,1) = pembulatan(new_rank(length(symbols)-j+1,1));
data_baru(iterasi,2) = new_rank(length(symbols)-j+1,2);
data_baru(iterasi,3) = new_rank(length(symbols)-j+1,3);
iterasi = iterasi +1;
end;
end;
end;
p = data_baru(:,1)';
sta=[];
ar=[];
coded=[];
other=[];
first=0;
final=length(p);
sta=[first final]
for f=1:length(p) %loop for every step
other=[];
ar=[];
for h=1:(length(sta)-1) %loop for each sub-group
first=sta(h)+1
final=sta(h+1)
if(first>=final) %adding invalid numbers(here 2)
other=[other; 2]; %-when only one element in a sub group
continue;
end
asum=0;
difmat=[]
for i=first:final %loop for finding difference vector
asum=asum+p(i);
resum=0;
for j=i+1:final;
resum=resum+p(j);
end
dif=abs(asum-resum);
difmat=[difmat dif]
end
small=min(difmat);
k=1;
for i=first:final %loop for finding index of min difference
if(small==difmat(k))
break;
end
k=k+1;
end
index=i
ar=[ar index] %storing index in temporary stack
ind=(index+1)-first %calulating number of zeros
remind=final-index %-and ones for each sub group
other=[other; zeros(ind,1); ones(remind,1)]
end
sta=[ar sta] %creating final stack for each step
sta=sort(sta)
coded=[coded other] %creating final code word matrix
if(length(sta)>length(p)) %break when all sub groups have one element
break;
end
end
%display codewords
len=[];
for i=1:length(p)
word=[];
for(j=1:f) % 'f' contains max number of bits among codes
if(coded(i,j)==2) % break when invalid number(i.e. 2) reached
break;
end
word=[word coded(i,j)];
end
len=[len length(word)];
fprintf('\nSymbol %d code ==> ',i);
disp(word)
code_final{i} = concat_array(word);
fi_final(i) = data_baru(i,1);
char_final(i) = data_baru(i,2);
freq_final{i} = strcat(': ',num2str(data_baru(i,3)),'/',num2str(sum(data_baru(:,3))));
li_final(i) = size(word,2);
si_final{i} = char(data_baru(i,2));
end
for i=1:length(p)
expansi_binner_final{i} = expansi_binner(code_final{i},code_final);
end;
cum_pi_final(1)=0.000;
for j=2:length(si_final);
cum_pi_final(j)=cum_pi_final(j-1)+fi_final(j-1);
end
clc;
fprintf('\nKEYWORD = %s',keyword);
fprintf('\nKEYWORD LENGTH = %i ',length(char(keyword)));
fprintf('\nUNIQUE = %s ',symbols);
fprintf('\nUNIQUE LENGTH = %i \n\n',length(char(symbols)));
DataHead = {'Si', 'Pi','Li','kode'};
%DataBody(:,1) = si_final'
%DataBody(:,2) = pi_final'
%DataBody(:,3) = code_final'
%DataBody(:,4) = li_final'
fprintf(1, ' \t %s\t\t\t\t %s\t\t\t %s\t\t \t%s\n', DataHead{:})
for i=1:length(si_final)
fprintf(1, '\t%s %s \t\t %.3f\t\t %d\t\t\t %s\n', si_final{i},freq_final{i},fi_final(i),li_final(i),code_final{i})
end;
ent=0;
avginfo=0;
for i=1:length(p)
ent=ent+(p(i)*log2(1/p(i)));
avginfo=avginfo+len(i)*p(i);
end
eff=(ent/avginfo)*100;
fprintf('\nENTROPY = %.3f',ent);
fprintf('\nAVERAGE CODEWORD LEGHT = %.3f',avginfo);
fprintf('\nCODING EFFICIENCY = %.3f%s \n',eff,'%');
disp(['Duration = ',num2str(toc),' second'])

1 Comment

pembulation is not a MATLAB function.
It looks like it might be an Indonesian word for rounding:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!