Matlab sections 1-5

Section 1: Accessing Matlab
Once logged into the system being used, Matlab can be entered and exited using the matlab and quit system commands. When using certain systems, such as Unix systems and MS Windows, it is suggested that the user keep both the Matlab and local editor active.

Section 2: Entering Matrices
Matlab is a matrix manipulator. Matrices can be 1x1 matrices, scalars, matrices with one column or one row, vectors, or any variation of columns and rows. The inputs can be integers, variables or complex numbers.

Inputting matrices into Matlab can be done in many different ways. They can be input with an explicit list of elements, built in statements or functions, diskfile from local editor or from external data files or applications.

The following are examples of matrix inputs.

Complex Numbers can be put into matrices in the following ways.

If an external data file is used. The file can be loaded into Matlab using the load function. For example, if the file is named data.ext. The file can be uploaded into Matlab using the function load data.ext.

There are many built in function in Matlab that can be used to generate matrices. This section will look at three common matrix generators, rand, magic and hilb.

rand(n) generates a matrix with random entries between 0 and 1 into a nxn matrix. rand(m,n) generates a matrix with rando0m entries between 0 and 1 into an mxn matrix Below is an example of the rand function.

magic(n) will create an integral nxn matrix which is a magic square. This means that the rows, columns and diagonals have common sums. The following is an example of the magic(n) function.

hilb(n) will create a nxn hilbert matrix (m and n denote positive integers).

The following is an example of the hilb(n) function.

Matrix and vector entries can be referenced using A(3,2). This pulls the entry from the 3rd row and the 2nd column from matrix A.

The following is an example of how to reference an entry within a matrix.

Section 3: Matrix Operation, Array Operations
Section three references the basic matrix operators. These operators are addition,+, subtraction,-, Multiplication,*, power,^, transpose,', and left and right division,/ and \. These operators should be known by anyone with a programming background.

While using the multiplication, power and division operators, a period can be used that will make the operators operate entry-wise.

The following is an example of entry-wise operations.

Section 4: Statements, Expressions and Variables; Saving a Session
Matlab is an expression language. It evaluates and interprets expressions entered and produces matrices. Expressions are composed of operators, functions and variable names.

A Statement is normally terminated with the return button. Several statemnet can be place on the same line using commas or semicolons following the statements. Semicolons suppress the printing of the statement.

A session can be saved be using the save command and reloaded using the load command. The following are examples of the save and load commands. EDU>> save

Saving to: matlab.mat

EDU>> load

Loading from: matlab.mat

Section 5: Matrix Building Functions
This section references convenient matrix building functions.

The eye function give an identity matrix of a given matrix. The following is an example of the eye functiion. This example shows the identity matrix of matrix A. EDU>> eye(size(A))

ans =

1    0

0    1

The zeros function gives a matrix of zeros. The following is an example of the zero function.

EDU>> zeros(3,3)

ans =

0    0     0     0     0     0     0     0     0

The diag function creates or extracts diagonals. The following is an example of the diag function.

EDU>> diag(5)

ans =

5

Using two diag functions together a diagonal of a matrix can be retrieved. The following is an example of the diag(diag(A)) function that will retrieve the diagonal of the A matrix.

EDU>> diag(diag(A))

ans =

1.0000 + 5.0000i       0 0            4.0000 + 8.0000i

Matrices can also be built from blocks. The following is an example of a matrix built from matrix A, two zeros matices and one eye matrix. This function builds a 5x5 matrix.

EDU>> B=[A, zeros(3,2); zeros(2,3), eye(2)]

B =

1    0     0     0     0     0     1     0     0     0     0     0     1     0     0     0     0     0     1     0     0     0     0     0     1