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.

Preparations
First use putty to connect to student machine, make sure your login ID and password work, then type MATLAB to make sure MATLAB works.  Then leave the session on student.  We will use MATLAB on PC's to get this lab done.  You can do some of this on student though. To start MATLAB in a Unix environment such as student (student.cs.appstate.edu) machine, once you loged into the system, type:
% matlab
This will open MATLAB software.

It is best that you create a directory lab0 before we start.  If you do not wish to do so right now, you can do this later on during the lab.  I assume, you are completing this lab on a PC.   You are required to answer some questions and e-mail the answer either as an attachment or as the body of an e-mail.  Here is the questions in plain text and in MS Word format.  My e-mail address is rt@cs.appstate.edu.

We will use the MATLAB Neural Network Toolbox in this course.  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 the commands and will also review some of the basic concepts of linear algebra.

To start MATLAB on a PC double click on the MATLAB icon.

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

1. Write the result on the answer-sheet.

In this case the step size is 0.5.  There are many built-in functions in MATLAB, for example: sin, cos, sqrt, .... MATLAB also 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 example, in array t the 3rd member is t(3)
2. What is the value of t(3)

To find the answer simply type:

>> t(3)

As you may have noticed, in MATLAB the array index starts from 1.

As we discussed in class when you multiply two matrices, you have to be careful about the dimension.  For example, 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 that the two middle numbers 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 a piece of paper. Here is how we create the transpose of t in MATLAB:

>> t_transpose = t'

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

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

>> sum = sum(t)

3. Compute the average, median, maximum, and minimum for array t.  If you need help, use the help at the MATLAB prompt or use the online PDF document on our web site.

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 carriage return.  Let's look at an example.

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

and another one with the same size, B, defined as;

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 a few of the linear algebra operations 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

4. Multiply matrix A by matrix B on paper.

>> 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 (a file with .m extension), 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.

## Creating .m files

Open an editor screen and type the following code segment in there.  On PCs, the Editor Screen can be opened under File, then New, the M-File.  On Unix, you need to use an editor of your choice (vi, vim, pico...).
% Lab(0) - lab0sample.m
A = [20 22 18; 12 16 12; 8  10  10]
B = [2 4 8
1 -1 6
-2 2 4]

C = A'
D = A*B

Save the file in the lab0 working directory.  Once you saved the file, make sure to change the Current Directory (at the top) to the directory in which you have saved the file.  Type the file name without the .m at the end.   This should run your script.  If it didn't run it, there is a possibility that your Current Directory is not correct.  To check where your are you can use the Unix like command pwd.  Also, you can use the Unix-like command ls to list the contents of the directory.

5. Modify the above program (include the program in your answer sheet) such that it computes 10*A + B.  Show the results in your answer sheet.

6. Modify the above program (include the program in your answer sheet) such that is computes a = W*P + B.  W is the same as A. P and B are defined as:
P = [ 2, 4, 6] '  and B = [-234, -155, -117] .  Show the result in your answer sheet.

7.  After you run the above case, try this, a = hardlims(a), what did this function do? Show your answer.

8.  After you run the above case, try this, a = hardlim(a), what did this function do? Show your answer.

Post-lab Assignment (Due Thursday June 1)  - Assignment (3)
Submit the commands and the results for each question as an e-mail attachment.  Please make sure your answer are separated by the question number.

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) What is the CROSS product of these two vectors.

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