huffmandict() The sum of elements of the probability vector must be 1

조회 수: 4 (최근 30일)
Amjad
Amjad 2014년 2월 27일
댓글: Amjad 2014년 2월 27일
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)

채택된 답변

Roger Stafford
Roger Stafford 2014년 2월 27일
편집: Roger Stafford 2014년 2월 27일
I see two possibilities here. Either 1) there is some discrepancy between the 'totalCharz' value and the actual sum of 'countMatrix' values, which you should be able to check easily, or 2) the 'huffmandict' function is requiring an exact sum of 1 rather than allowing a tolerance for tiny round-off errors in the sum of probabilities.
If it is 1), the remedy is obvious: use sum(countMatrix) instead of totalCharz. Perhaps a character was encountered which is not one of your 37. If it is 2), and the difference between the actual sum and a perfect 1 is very tiny, make an adjustment to one or more of your probability values such as to produce an exact 1.
I will guess the problem is 1). It seems unreasonable to me that Mathworks would require an exact sum of 1 in 'huffmandict' without some allowance for round-off error.
  댓글 수: 3
Roger Stafford
Roger Stafford 2014년 2월 27일
Well, if 'huffmandict' allows an error of 1*e-6, that should easily be satisfied by your code if 'totalCharz' were correct. Conclusion: The problem lies with 'totalCharz'. I would again strongly recommend you use sum(countMatrix) instead of 'totalCharz'. Don't be afraid. Try it!
Amjad
Amjad 2014년 2월 27일
You were right. the length and sum were different.
length(inputData) =1096742
sum(countMatrix) = 1096739
I guess it was due to 2 unknown characters that were not in the string. Now i have converted to
totalCharz = sum(countMatrix)
and it works.
Thanks for the help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by