**Run-Lenght Encoding** is: instead of saying “apple apple apple pear pear banana orange orange apple”, you simpily say “Three apples, two pears, one banana, two oranges, and one apple. RLE is as simple as that.

Of course, in the world of digital computers you don’t count apple, pears or bananas. Digital computer use 0 and 1 to represent everything, so you count 0 and 1 instead. And the following is a simple and real world **run-length encoding example**

Run-length encoding (RLE) is a very simple form of 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.

**run length encoding algorithm** is fairly simple and straight forward to implement and that’s the becauty of run length encoding compression.

## Run Length Encoding Examples

**Run Length Encoding Algorithm Matlab Implementation**

*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*

**Run Length Decoding Java Example Code**

*public class RunLength{ private final static int R = 256; //max run length count private final static int lgR = 8; //number of bits per count public static void expand(){ boolean bit = flase; while(!BinaryStdIn.isEmpty()){ int run = BinaryStdIn.readInt(lgR); //read 8-bit count from stdin for (int i=0; i<run; i++){ BinaryStdOut.write(bit); //write 1 bit to stdout } bit = !bit; } BinaryStdOut.close(); //pad 0s for byte alignment } }*

**Run-Length Coding Interactive Visual Demo **http://demonstrations.wolfram.com/BinaryRunLengthEncoding/

**Run-Length Coding Video**

http://www.youtube.com/watch?v=J5jEs8942xI

http://www.youtube.com/watch?v=nxtu-4rG8CQ

**JPEG Video**

http://www.youtube.com/watch?v=jZogvFWSRiQ