from Arithmetic coding and decoding for Text Compression by Madhu S. Nair
Arithmetic coding and decoding for Text Compression

[alpha cnt]=probmodel(seq)
function [alpha cnt]=probmodel(seq)
 if ~isempty(seq)
     alpha(1)=seq(1);
     cnt(1)=1;
     l=length(seq);
     k=2;
     for i=2:l
         idx=find(seq(i)==alpha);
         if isempty(idx)
             alpha(k)=seq(i);
             cnt(k)=1;
             k=k+1;
         else
            cnt(idx)=cnt(idx)+1; 
         end
     end
   else
     alpha=[];
     cnt=[];
 end
end

Contact us at files@mathworks.com