Shannon fano code error

5 views (last 30 days)
Maheen
Maheen on 15 Oct 2015
Edited: Geoff Hayes on 20 Oct 2015
function [sorted1 sorted2] = shannon_fano(keyword)
probabilities_calculation = zeros(size(keyword));
for i = 1:length(keyword) %find the probabilities of the symbols/occurence of each letter
probabilities_calculation(i) = (sum(keyword==keyword(i))/length(keyword));
end
[sorted1 sorted2] = shannon_split(probabilities_calculation);
function [first_half second_half] = shannon_split(input_symbols)
add_prob = 0;
for k = 1:length(input_symbols)
symbol1 = zeros(size(input_symbols));
if(input_symbols > 1)
add_prob = add_prob + input_symbols(k);
symbol1(k) = input_symbols(k);
if(sum(input_symbols)- add_prob <= 0.5)
first_half = zeros(size(symbol1(symbol1~=0)));
symbol2 = input_symbols(1:end) - symbol1; %now for the second half we consider the remaining elements
updated = symbol2(symbol2~=0);
second_half = ones(size(updated));
shannon_split(first_half);
shannon_split(second_half);
end
end
end
This is my code for shannon fano but i am receiving an error in it and is unable to solve it :(
This is the error:
Error in ==> shannon_fano>shannon_split at 15
add_prob = 0;
??? Output argument "first_half" (and maybe others) not assigned during call to
"D:\Matlab\shannon_fano.m (shannon_split)".
Error in ==> shannon_fano at 11
[sorted1 sorted2] = shannon_split(probabilities_calculation);
  1 Comment
Maheen
Maheen on 15 Oct 2015
oh got it so if i use while instead of if then would it be fine? as i am getting error in that too

Sign in to comment.

Answers (1)

Thorsten
Thorsten on 15 Oct 2015
if(sum(input_symbols)- add_prob <= 0.5) if false, first_half and second_half are not computed in shannon_split, so the output arguments are not computed.
  3 Comments
Thorsten
Thorsten on 15 Oct 2015
You have to ensure that for all input_symbols the values of both output parameters of your function are defined. So for this is only the case if at least once in your for loop the condition
(sum(input_symbols)- add_prob <= 0.5) i
becomes true.
Maheen
Maheen on 15 Oct 2015
function mid = division(probabilities)
add_prob = 0;
for k = 1:length(probabilities) %go through the entire set
add_prob = add_prob + probabilities(k); %add single probability at a time
mid(k) = probabilities(k); %save the no of added probabilities
if(sum(probabilities)-add_prob <= 0.5)
break;
end
end
This is a code which i have improved for the division of probabilities so now where i can use the recursion technique so that i could repeat this task until we have only one element left

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!