A Padding Function

In this note we describe a simple function which takes as input a binary vector of length less than or equal to 8 and returns that vector padded with as many leading zeros as necessary to have a length of exactly eight. For example, if the input is

        [1 1 0 1 0]
the output is
        [0 0 0 1 1 0 1 0]
Matlab code for implementing this function is as follows:
   % pad a binary vector with less than 8 bits - add zeros to the right
   function n = pad8(n)
      for i=7:-1:length(n)
         n = [0 n];

Here is an explanation of the code, line by line: