Run Length Encoding & Decoding in MATLAB
Run-length encoding, or RLE, is a straightforward method of lossless data compression in which runs of data, or sequences of data with the same value in many consecutive elements, are stored as a single value and count rather than as the original run. In other words, RLE ( Run Length coding) is a straightforward technique for data compression that involves specifying the number of times a characterâs or pixelâs color repeats before the characterâs or pixelâs value. The goal is to use fewer bits to represent a given set of data. This method should only be used if the number of occurrences is equal to or more than four to get the benefit of compression. This method can also be optimized for replacing a group of repeated characters instead of single character.
Run Length Encoding:
By âcountingâ the entries in a given sequence, a counting sequence (RLE) is created.
For example, the sequence x = 2, 2, 2, 1, 1, 5, 5, 6, 6, 6, 6. can be read as Three 2âs, Two 1âs, five 2âs, four 6. which translates to b = 3, 2, 2, 1, 5, 2, 4, 6. Therefore, b is the counting pattern for a.
Example 1:
Matlab
% MATLAB CODE for encoding clc clear all close all a=input( 'Enter the array:' ); b=[]; c=1; for i=1:length(a)-1 if (a(i)==a(i+1)) c=c+1; else b=[b,c,a(i),]; c=1; end end b=[b,c,a(length(a))]; disp(b); |
Output:
For example, the sequence x = 9, 5, 1, 2, 3, 4, 5, 7 and 2, 5, 1, 2, 4, 1, 1, 3 can be read as Nine 5âs, one 2, three 4âs, five 7. Which translates to y = 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 4, 4, 4, 7, 7, 7, 7, 7
Example 2:
Matlab
% MATLAB code for decoding clc clear all close all a=input( 'Enter the array:' ); b=[]; for i=1:2:length(a) b=[b a(i)]; end c=[]; for i=2:2:length(a) c=; end u=[]; for i=1:length(b) n=b(i); r=c(i); for q=1:n u=[u r]; end end |
Output:
Contact Us