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]; end end

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

`function n = pad8(n)`: This is a declaration of the`function`which will be known to the world as`pad8`. The piece`(n)`says the function is to take one input argument and it will be called`n`*within the body of the function only*. The value of`n`will be completely unknown to the world outside the function. The body of the function will expect`n`to be a vector - if it isn't there may be dire consquences. The piece`n =`means that when the function terminates, whatever the value of`n`is at that point, that value is the value returned by the function. There is no need to use a`;`at the end of the line since the line is not executable and does not have a value to output.`for i=7:-1:length(n);`: The function operates as follows: it counts down in increments of 1 from 7 to the length of`n`and, for each increment, places a`0`at the beginning of`n`. This is a repetitive process that is naturally handled by a`for`statement. The number of iterations of the`for`statement is 8-`length(n)`. For example, if`length(n)`is 5, the`for`loop iterations will have`i=7`, then`i=6`, then`i=5`, so there will be 3 iterations - observe 8-`length(n)`= 8-5 = 3. There is no need to use a`;`at the end of the line since the line does not have a value to output.`n = [0 n];`: Every time this line is executed a`0`is added to the front of the vector`n`. The`for`causes this line to be executed 8-`length(n)`times - hence this is how many`0`s will be added to the front. The total length of`n`when finished will be (8-`length(n)`)+`length(n)`= 8. There is a`;`at the end of this line to suppress printing the value of`n`as it is updated by the assignment statement.`end`: Defines the body of the`for`loop. The loop begins at`for i=7:-1:length(n)`and ends at the first matching`end`(which is this one). There is no need to use a`;`at the end of the line since the line does not have a value to output.`end`: Defines the body of the function`pad8`which begin with the`function`line. There is no need to use a`;`at the end of the line since the line does not have a value to output.