Shannon fano code error

7 visualizaciones (últimos 30 días)
Maheen
Maheen el 15 de Oct. de 2015
Editada: Geoff Hayes el 20 de Oct. de 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 comentario
Maheen
Maheen el 15 de Oct. de 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

Iniciar sesión para comentar.

Respuestas (1)

Thorsten
Thorsten el 15 de Oct. de 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 comentarios
Thorsten
Thorsten el 15 de Oct. de 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 el 15 de Oct. de 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

Iniciar sesión para comentar.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by