Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Run Length Encoding & Decoding in MATLAB

Run-length encoding (RLE) is a very simple form of lossless data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. This is most useful on data that contains many such runs.

For more detail & examples , check the below link:


Encoding in MATLAB:

A counting sequence(RLE) is formed by "counting" the entries in a given sequence.

For example, the sequence x = 5, 5, 2, 1, 1, 1, 1, 3


can be read as Two 5's, one 2, four 1's, one 3


which translates to y = 2, 5, 1, 2, 4, 1, 1, 3


So y is the counting sequence for x.

Following is the MATLAB CODE which can be implemented for the encoding purpose:

clc clear all close all x=input('Enter the array:'); y=[]; c=1; for i=1:length(x)-1 if(x(i)==x(i+1)) c=c+1; else y=[y,c,x(i),]; c=1; end end y=[y,c,x(length(x))]; disp(y);

Explanation:


Sample Output:


Decoding in MATLAB:

For example, the sequence x = 2, 5, 1, 2, 4, 1, 1, 3

can be read as Two 5's, one 2, four 1's, one 3

which translates to y = 5, 5, 2, 1, 1, 1, 1, 3

MATLAB CODE for Decoding:

clc
clear all
close all
x=input('Enter the array:');
y=[];
for i=1:2:length(x)
    y=[y x(i)];
end
z=[];
for i=2:2:length(x)
    z=[z x(i)];
end
v=[];
for i=1:length(y)
    m=y(i);
    q=z(i);
    for j=1:m
        v=[v q];
    end
end
        
Explanation:


   
Sample Output:


No comments

Popular Posts