huffman.m

施令雪
2023-12-01
function CODE =huffman(p) 
error(nargchk(1,1,nargin));
if (ndims(p)~=2) ||(min(size(p))>1)||~isreal(p)||~isnumeric(p)
   error('P must be a real numeric vector.'); 
end 
global CODE
CODE=cell(length(p),1);
if length(p)>1
    p=p/sum(p);
    s=reduce(p);
    makecode(s,[]);
else
    CODE={'1'};
end

function s = reduce(p)
s=cell(length(p),1);
for i=1:length(p)
    s{i}=i;
end
while numel(s)>2
    [p,i]=sort(p);
    p(2)=p(1)+p(2);
    p(1)=[ ];
    s=s(i);
    s{2}={s{1},s{2}};
    s(1)=[ ];
end

function makecode(sc,codeword)
global CODE
if isa(sc,'cell')
    makecode(sc{1},[codeword 0]);
    makecode(sc{2},[codeword 1]);
else
    CODE{sc}=char('0'+codeword);
end

 

 类似资料:

相关阅读

相关文章

相关问答