from RUN LENGTH ENCODING by Shoeb Temrikar
rle is performed on a binay stream of data

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

Contact us at files@mathworks.com