/*MATLAB PROGRAM RUN LENGTH ENCODING*/
%
Run Length Encoder
%
EE113D Project
function
encoded = RLE_encode(input)
my_size
= size(input);
length
= my_size(2);
run_length
= 1;
encoded
= [];
for
i=2:length
if
input(i) == input(i-1)
run_length
= run_length + 1;
else
encoded
= [encoded input(i-1) run_length];
run_length
= 1;
end
end
if
length > 1
% Add last value and run length to output
encoded
= [encoded input(i) run_length];
else
% Special case if input is of length 1
encoded
= [input(1) 1];
end
OUTPUT
>>RLE_encode([1
0 0 0 0 2 2 2 1 1 3])
ans
=
1
1 0 4
2 3 1
2 3 1
>>RLE_encode([0
0 0 0 0 0 0 0 0 0 0])
ans
=
0
11
>>RLE_encode([0
1 2 3 4 5 6 7 8 9])
ans
=
0
1 1 1
2 1 3
1 4 1
5 1 6
1 7 1
8 1 9
1
No comments:
Post a Comment