📄 rle_encoding.m
字号:
%
%{
RUN LENGTH ENCODING
rle is performed on a binay stream of data
where in the 1st element is copied (same) as the binay data,
the next element is the no of times the first element,
the next element is the no of times the next binary logic data and so on
eg a=[0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1]
rle =[0<<1stbinary bit> 1<<no of 0's> 2<<no of 1's> 1<<no of 0's> 1<<no of 1's> 1 5 7 2 1 1]
%}
a=input('enter any binay stream: ');
% works any format viz... double as well as logical format
% eg: [0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1];
% eg: logical([0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1]);
n=2;
% this loop calculates the length <n> of run length encoding matrix on the
% input stream of binary data
% by checking no of times the logic level changes
for k=1:length(a)-1
if a(k)~=a(k+1)
n=n+1;
end
end
rle=ones(1,n); % Preallocate array +using memory efficiently
rle(1)=a(1);
m=2;
for i=1:k
if a(i)==a(i+1)
rle(m)= rle(m)+1;
else m=m+1;
end
end
display(rle); %is of type double
% the code is self explanatory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -