Lab (0) - Neural Network
Matlab
Arrays and their operations
Matrix and their operations
Some Linear Algebra in Matlab

In this lab you will learn about some of the commands used in Matlab.



In this course we are using the Matlab Neural Network Toolbox.  Matlab is a matrix-based application.  We will utilize the tools and commands that are already built into the Matlab Neural Network Toolbox to do some simulations.   In this lab, we will learn some of those commands and we also review some of the basic concepts in linear algebra.

In order to see some of the built-in functions in Matlab at at the Unix prompt type:
% matlab
This will open Matlab software.

Arrays (Vectors)

A vector is what we usually refer to as a one-dimensional array (1-D array).  The array can be of different types.

Creating an array (1-D matrix):
To create an array, t, with five elements (1 through 5) use:

>> t = [1 2 3 4 5]

This creates the array t with 5 members, 1, 2, 3, 4, and 5.  Note that in this case all 5 members are sequential.  We could use:

>> t = 1:5

to create the same array.

We can also create an array by giving the beginning number, the end number and an step size (increment).  In the above example, the step size is 1, thus, we could do the following:

>> t = 1:1:5

In this case the middle number is the step size. Try the following command to see the difference:

>> t = 1:0.5:5

In this case the step size is 0.5.  There are many built-in functions in Matlab, for example: sin, cos, sqrt, .... Matlab even allows you to add your own function.

Note that the first array member in the Matlab is t(1).  In order to access a particular member of an array we use the index corresponding to that member.  For instance, in array t the 3rd member is t(3).  What is the value of t(3)?  To find the answer simply type:

>> t(3)

answer :
Why?

As we discussed in class when you multiply two matrices, you have to be careful about the dimension.  For instance, if a matrix A is of size n by 3, then it only can be multiplied by a matrix of size 3 by m .  Note that n and m are arbitrary numbers and the only requirement is the two middle number to be the same.  Thus, number of columns in the first matrix must be the same as number of rows in the second matrix.

Suppose we want to multiply t by itself.  You have already generated t as an array with one row and 9 columns.  Thus, you have to write the t as a 9 row by 1 column to be able to multiply it by itself.   Therefore, we need to transpose the second t.

To compute the transpose of a matrix we will use, '.  Write the transpose of matrix t on the side of this page. Here is how we create the transpose of t in Matlab:

>> t'

Transpose of a matrix is the same matrix when rows and columns are switched.

There are some powerful operators and functions in Matlab that makes our life much easier.  For instance, if you want to compute the sum of all members in t, you can use the sum function:

sum = sum(t)

Check your answer to make sure it is correct.

Compute the average?  Write the command below and try to see if it works correctly?
 
 

There are many different commands in Matlab that we will use throughout this semester.  One powerful tool in Matlab is the plotting routine.  To plot t, you can simply run:

>> plot(t)

Of course, you can use help plot to learn about different options that you can use with plot.

Note: in the above example, although t was an array we call it a matrix.  An array can be viewed as a matrix with one row and some columns.

Matrices

A matrix is what we refer to as an array with more than one dimensions.  There are many commands designed in Matlab to do matrix manipulation.  Entering matrices into Matlab is the same as entering a vector, except each row of elements is separated by a semicolon (;) or a return.  Let's look at an example.

Suppose we have a 3x3 matrix, A, defined as;

A =
20 22 18
12 16 12
8  10  10

and another one with the same size, B, defined as;
B =
2 4 8
1 -1 6
-2 2 4

How do we enter these matrices in Matlab.
First method:
A = [20 22 18; 12 16 12; 8  10  10]

The output will looks like this:
A =
    20    22    18
    12    16    12
     8    10    10

Second method:
B = [2 4 8
1 -1 6
-2 2 4]

The output looks like this:
B =
     2     4     8
     1    -1     6
     -2     2     4

Let's try few of the operations in linear algebra here.  Suppose, you want to find the transpose of matrix A, rows are interchanged with columns, and call the new matrix C.
>> C = A'
This generates the following output;
C =
    20    12     8
    22    16    10
    18    12    10

Suppose, we want to compute D = A * B.
Do you remember how that was done in linear algebra?

>> D = A*B
D =
    26    94   364
    16    56   240
    6    42   164

In this lab, you tried several of the Matlab commands individually. You could put all of the commands that you want to execute together in an m-file, and just run the file. If you put all of your m-files in the same directory that you run Matlab from, then Matlab will always find them. To create an m file, use an editor to type the commands and once you are done, save the file with .m extension.

A simple Neural Network
We said in class that the output of a neural network can be determined as:
a = f (n)
a = f ( wp + b )
Where f is the activation function. Now let's assume we have a network with one neuron and 5 inputs. The input is defined as:
p = [89 79 78 90 60]
w = [.2 .2 .2 .2 .2]
Use a hardlim function to start with and play with the bias until this network produces 1 for values of a that is larger than 75.
Note: this is only possible when you use different values for b until the network produces 1. But a large value of b is not desirable, so you need to find the smallest value of b that produces 1 for values larger than or equal to 75. For now assume b is an integer.

Homework: (Due Wednesday June 2)
1) How do you multiply the corresponding elements of matrix A and B in the above example?  Please note that I didn't ask you to multiply A * B.  I want you to tell me how to multiply A(1,1) by B(1,1), and A(1,2) by B(1,2), ....?  Find the command or the operator that does this for you and compute the result for the above two matrices.

2) Suppose there are two vectors in the 3-D space (x, y, z) defined as:

A = 3i + 2j - 4k
B = -4i + 3j + 2k

i, j, k are the unit vectors representing the x, y, and z coordinates respectively.  What is the DOT product of these two vectors.

3) Compute the variance and standard deviation for vector t that is created using t = 1:0.25:5.