Shannon fano code error

9 次查看(过去 30 天)
Maheen
Maheen 2015-10-15
编辑: Geoff Hayes 2015-10-20
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 个评论
Maheen
Maheen 2015-10-15
oh got it so if i use while instead of if then would it be fine? as i am getting error in that too

请先登录,再进行评论。

回答(1 个)

Thorsten
Thorsten 2015-10-15
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 个评论
Thorsten
Thorsten 2015-10-15
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 2015-10-15
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

请先登录,再进行评论。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by