LZW编码算法,尝试使用matlab计算%encoder LZW for matlab%yulc;clear;close all;%初始字典dic = cell(512,1);for i = 1:256dic{i} = {num2str(i)};end%输入字符串a,按空格拆分成A,注意加1对应范围1~256a = input('input:','s');a = deblank(a);A = regexp(a,'\s+','split');L = length(A);for j=1:LA{j} = num2str(str2num(A{j})+1);endA_t = A{1};%可识别序列B_t = 'test';%待验证词条d = 256;%字典指针b = 1;%输出指针B = cell(L,1);%输出初始output = ' ';%输出初始j=1;for j = 2:Lm=1;B_t =deblank([A_t,' ',A{j}]);%合成待验证词条while(m <= d)if strcmp(dic{m},B_t)A_t = B_t;breakelsem=m+1;endendwhile(m == d+1)d = d+1;dic{d} = B_t;q=1;for q=1:dif strcmp(dic{q},A_t)B{b} = num2str(q);b = b+1;endendA_t = A{j};endendfor q=1:d%处理最后一个序列输出if strcmp(dic{q},A_t)B{b} = num2str(q);b = b+1;endendfor n = 1:(b-1)B{n} =num2str(str2num(B{n})-1);output=deblank([output,' ',B{n}]);endoutput运算结果计算结果为3939126 126 256 258 260 259 257 126LZW解码算法,使用matlab计算%decoder LZW for matlab%yulc;clear;close all;%初始字典dic = cell(512,1);for i = 1:256dic{i} = {num2str(i)};end%输入字符串a,按空格拆分成A,注意加1对应范围1~256a = input('input:','s');a = deblank(a);A = regexp(a,'\s+','split');L = length(A);for j=1:LA{j} = num2str(str2num(A{j})+1);endB_t = A{1};%待验证词条d = 256;%字典指针b = 1;%输出指针B = cell(L,1);%输出初始output = ' ';%输出初始j=1;B{b} = char(dic{str2num(A{j})});b = b+1;for j = 2:LBB = char(dic{str2num(A{j})});B_d = regexp(BB,'\s+','split');%按空格拆分L_B = length(B_d);p=1;for p=1:L_BB{(b+p-1)} = B_d{p};m=1;B_t =deblank([char(B_t),' ',char(B_d{p})]);%合成待验证词条while(m <= d)if strcmp(dic{m},B_t)B_t = B_t;breakelsem=m+1;endendwhile(m == d+1)d = d+1;dic{d} = B_t;B_t = B_d{p};endendb = b+L_B;end for n = 1:(b-L_B)B{n} = num2str(str2num(B{n})-1);output=deblank([output,' ',B{n}]); end output运算结果运算结果为 3939126126393912612639391261263939126126
lzw编码 matlab,LZW编码算法matlab实现.docx
最新推荐文章于 2022-05-16 15:20:05 发布