Shannon fano code error
Show older comments
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);
Answers (1)
Thorsten
on 15 Oct 2015
0 votes
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
Maheen
on 15 Oct 2015
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
on 15 Oct 2015
Categories
Find more on Code Performance 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!