% Program to find the arithmetic code of a sequence.
% set x, the sequence of integers and
% count, the number of times each integer occurs.
% outputs the binary code and the wordlength used.
% decodes the binary code to get back the input
clear
x=[9 9 1 9 9 4 8 1] % input sequence
lseq=length(x); % length of the sequence
y=unique(x) % list of unique symbols
count=[10 2 5 9] % number of times each symbol occurs
c_count=cumsum([0 count]) % cumulative count
[code,N]=a_encode(x,y,c_count) % calling encoding function
xr=a_decode(N,y,c_count,code,lseq) % calling decoding function