• 大小: 4KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: 其他
  • 标签:

资源简介

只统计英文文本的26个字母(不分大小写)出现概率,并对这26个字母进行哈夫曼编码,香农编码,主程序在CalculateLetters_Number.m中。

资源截图

代码片段和文件信息

clc
clear
 %导入pages.txt文件,获取文章中的字符串
fid = fopen(‘a.txt‘);               
tline = fgetl(fid);
%变量初始化
i = 1;
test = [];
j = 1;
str1 = ‘‘;

while ischar(tline)
    disp(tline);                    %显示每一行
    pages{i} = tline;               %将文章中的每一行的字符串存在数据类型为元包(cell)变量中
    tline = fgetl(fid);
    i=i+1;
end
fclose(fid);

%for循环?实现pages中的所有行字符串存储在同一个名为:str的字符串变量中
for i=1:length(pages)
    str = strcat(str1pages{i});
    str1 = str;
end
str;                               %拿str字符串,自行与文章全文进行校对
a = lower(str);                    %大写字母转小写
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%滤除其他字符,只计算英文字符
for i=1:length(a)
    if(a(i)>=‘a‘)
        if(a(i)<=‘z‘)
        test(j) = a(i);
        j = j + 1;
        end
    end
end
st=sprintf(‘%c‘test)              %显示最终要统计的英文字符
%%
char = unique(st);                 %计算有多少个不重复的字符串

for i = 1:length(char)
    char(i);
    number(i) = length(strfind(stchar(i))); %统计字符的数目
end
char                               %显示字符
number                             %显示个数

%计算频率frequency
frequency = number/sum(number)     %这里进行一个计算只为了与上面的number现实的个数相对应(这个frequency与下面那个frequency只是顺序不同)
%分别将所有出现过的字符:char每个char出现的次数:number每个char出现的频率:frequency
number = sort(number2‘descend‘) ; %做一个降序,方便下面进行编码
frequency = number/sum(number);     %再次计算频率
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%哈夫曼编码%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%哈夫曼编码主程序    
disp(‘%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%哈夫曼编码%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%‘)
r=2;                                %r代表r元码
Huffman_CLQ(frequencyr)
disp(‘%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%香农编码%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%‘)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%香农编码%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
shannon_result = shannon(frequency)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        145  2018-01-11 14:55  上传文件\a.txt

     文件       2265  2018-01-12 01:15  上传文件\CalculateLetters_Number.m

     文件        276  2018-01-11 15:25  上传文件\check.m

     文件        253  2018-01-11 15:27  上传文件\first_S.m

     文件       2407  2018-01-11 15:58  上传文件\Huffman_CLQ.m

     文件       1661  2018-01-12 01:16  上传文件\shannon.m

     文件        919  2018-01-11 15:46  上传文件\yasuo_pp.m

     目录          0  2018-01-11 15:58  上传文件

----------- ---------  ---------- -----  ----

                 7926                    8


评论

共有 条评论