huffmandict() The sum of elements of the probability vector must be 1
Show older comments
I am trying to use the huffmandict() function and it works if i use the sample program but gives me the error when i run this program
clear all; close all; clc;
load 'probMatrix.mat'
symbols = {' ','0','1','2','3','4','5','6','7','8','9','A','B',...
'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',...
'T','U','V','W','X','Y','Z',};
p = probMatrix;
[dict,avglen] = huffmandict(symbols,p)
samplecode = dict{5,2}
The probability matrix is calculated here.
Charz = ' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
fi = fopen('testdata.txt');
inputData = fread(fi,'*char');
inputData = inputData';
totalCharz = length(inputData);
countMatrix = zeros(1,37);
probMatrix = zeros(1,37);
for count=1:length(Charz)
x = strfind(inputData,Charz(count));
charCount = length(x);
% Saving Count
countMatrix(1,count) = charCount;
end
% Finding Probabilities
probMatrix = countMatrix/totalCharz;
The sum(probMatrix) also gives the ans 1.0000 but i still get the error.
Error using huffmandict (line 107)
The sum of elements of the probability vector must be 1
Error in SampleHuffman (line 10)
[dict,avglen] = huffmandict(symbols,p)
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!