huffman code impelementation manual not use the built in function

hi guys any one can help me in huffman code (data compression) to be more efficiency
this code is already working but i need it to be general and more efficiency i already use the built in code to insure
clc;
clear all;
close all;
l=6;
o=5;
matrix=[0.3 0.3 0.3 0.43 0.57 1;
0.25 0.25 0.27 0.3 0.43 0;
0.15 0.18 0.25 0.27 0 0
0.12 0.15 0.18 0 0 0;
0.1 0.12 0 0 0 0;
0.08 0 0 0 0 0;];
code=cell(6,6);
index=[3,2,1,1];
code{1,6}=1; %%%
code{1,5}=0; %%%
code{2,5}=1; %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
number1=2;
number2=3;
num1=2;
number=5; % number of colum
prob=[0.3 0.25 0.15 0.12 0.1 0.08 ];
[dict,avglen] = huffmandict(1:6,prob);
rr=[];
%%%% clear the zero in matrix %%%
for i=5:-1:2
a=intersect(matrix(:,i),matrix(:,i-1));%% intersect the the element
code{num1,number-1}=[0,code{index(i-1),number}];
code{num1+1,number-1}=[1,code{index(i-1),number}];
number=number-1;
num1=num1+1;
[ll,~]=size(a); %% size of output matrix
for ii=1:ll
a(a==0)=[]; %% clear zero in array
end
[l_new,~]=size(a); %% the new size of matrix after the clear operation
for iu=1:l_new
y=find(a(iu)==matrix(:,i)); %% find the element
rr=[rr y];
rr=sort(rr,'descend');
rr=flip(rr);
end
b=cell(1,l_new); %% create new cell have the index of element
%%%%%this is cmulative function according the index value%%%%
for iy=1:l_new
b{1,iy}=code{rr(iy),i};
for iv=1:l_new
code{iv,i-1}=b{1,iv};
end
end
rr=[];
b={};
end
%%%%%%%%%%%%%%%%%%%final answer to calculate the average length%%%%%%%%%%%
code=code(:,1);
length=[];
matrix=[];
for i=1:l
code{i}=flip(code{i});
[~,c]=size(code{i});
length=[length c];
s=code{i};
matrix=[matrix s];
end
avl=0;
for i=1:l
average_length=prob(i)*length(i);
avl=avl+average_length;
end

댓글 수: 7

for ii=1:ll
a(a==0)=[]; %%clear zero in array
end
Why is that a loop? After the first execution there would be no more 0 left.
Shehab Tarek
Shehab Tarek 2020년 5월 23일
편집: Shehab Tarek 2020년 5월 23일
to clear the zeros which are generate from intersect the colum 5 with colum 4 in matrix variable
LL not 11
a=[7 0 3 1 0 9]
a(a==0) = []
How many 0 are left in a?
a(a==0)=[]
how many more 0 did executing the statement again delete?
yes delete any zero number in array
final result
a=[7 3 1 9];
so how many loop iteration?
According to the size of array
no, after one iteration all of the remaining zeros are gone and you can stop.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2020년 5월 23일

댓글:

2020년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by