資源簡介
用matlab來實現n元的霍夫曼編碼,元數n任意輸入,概率個數任意輸入
代碼片段和文件信息
%哈夫曼編碼:
clcclear
P=[0.36?0.14?0.13?0.12?0.10?0.09?0.04?0.02];?
r=2;?
p=P;
n=length(p);
s=ceil((n-r)/(r-1));
a=(r-1)*s+r-n;
p=[pzeros(1a)];
n=length(p);
p=fliplr(sort(p));
T=p;
B=zeros(ns+1);
B(:1)=T;
?temp=sum(B(n-r+1:end1));
T(n-r+1)=temp;
?for?i=n-r+2:n
?????T(i)=0;
?end
?T=fliplr(sort(T));
?t=n-r+1;
for?j=2:s+1?
????for?i=1:t
????????B(ij)=T(i);
????end
????????K=find(T==temp);
????????B(nj)=K(end);?
????????temp=sum(B(t-r+1:tj));
????????T(t-r+1)=temp;
????????T=fliplr(sort(T));?
????????t=t-r+1;
end
- 上一篇:r元霍夫曼編碼的matlab實現
- 下一篇:隨機過程 布朗運動matlab仿真程序
評論
共有 條評論