rle_encoding.m

来自「run length encoding in matlab」· M 代码 · 共 38 行

M
38
字号

%
%{
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 + =
减小字号Ctrl + -
显示快捷键?