Реализовать функцию huffmandict () в matlab с использованием массивов - PullRequest
0 голосов
/ 19 декабря 2018

Я хотел бы реализовать функцию huffmandict () в Matlab.Я уже написал код, в котором я создаю массив со всеми вероятностями.Каждый раз, когда я добавляю 2 последние вероятности, я обновляю свой массив, добавляя новую сумму вероятностей в следующей строке в нужном месте.У меня также есть массив только с суммами.Проблема в том, что я не знаю, как продолжить присваивать «0» и «1».Любая идея?Это мой код:

function code_words = my_huffmandict_func(init_symbols,probs) 

my_symbol_array = [];
my_symbol_array = init_symbols;
my_probs = [];
my_probs = probs;

if length(my_symbol_array)~=length(my_probs)
 error('Number of symbols and number of probabilities are not the same.');
end

for i=1:length(my_probs) %sorting the probabilities in descending order and 
change the sequence of the symbols
for j=1:length(my_probs)
    if (my_probs(i)> my_probs(j))
        temp1=my_probs(i);
        temp2=my_symbol_array(i);
        my_probs(i)= my_probs(j);
        my_symbol_array(i)= my_symbol_array(j);
        my_probs(j)= temp1;
        my_symbol_array(j)= temp2;
    end
 end
end

my_sum_array = [];
k=1;
init_lengthpr = length(my_probs);
all_occured_probs = [];
all_occured_probs(1,:) = my_probs;

while length(my_probs)>2 %we need this while loop as long as there are more 
than 2 symbols left
my_temp_sum = my_probs(length(my_probs)) + my_probs(length(my_probs-1)); %we add the the possibilities of the two less possible outputs
my_sum_array = [my_sum_array,my_temp_sum]; %in this array we keep all the sums that occured
my_probs = [my_probs(1:length(my_probs)-2), my_temp_sum];%we update the possibilities' array
my_probs = sort(my_probs,'descend'); %we sort the array again
k=k+1;
all_occured_probs(k,:) = [my_probs,zeros(1,init_lengthpr-length(my_probs))];

end

end
...