Matlab sections 1-5

Section 1: Accessing Matlab edit

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 edit

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.

EDU>> A=[1 2 3;4 5 6;7 8 9];

EDU>> A=[

1 2 3

4 5 6

7 8 9]

A =

     1     2     3

     4     5     6

     7     8     9

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

EDU>> A=[1 2;3 4] +i*[5 6;7 8];

EDU>> A=[1+5i 2+6i;3+7i 4+8i]

A =

 

   1.0000 + 5.0000i   2.0000 + 6.0000i

   3.0000 + 7.0000i   4.0000 + 8.0000i

 EDU>> A=[1+5i 2+6i;3+7i 4+8i]

 A =

 

   1.0000 + 5.0000i   2.0000 + 6.0000i

   3.0000 + 7.0000i   4.0000 + 8.0000i

EDU>> A=[1 2;3 4] +j*[5 6;7 8]

 A =

 

   1.0000 + 5.0000i   2.0000 + 6.0000i

   3.0000 + 7.0000i   4.0000 + 8.0000i

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.

EDU>> rand(3)

ans =

 
    0.8147    0.9134    0.2785

    0.9058    0.6324    0.5469

    0.1270    0.0975    0.9575

EDU>> rand(3,4)

ans =


    0.9649    0.9572    0.1419    0.7922

    0.1576    0.4854    0.4218    0.9595

    0.9706    0.8003    0.9157    0.6557

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.

EDU>> magic(3)

ans =

     8     1     6

     3     5     7

     4     9     2

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

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

EDU>> hilb(3)

 ans =

    1.0000    0.5000    0.3333

    0.5000    0.3333    0.2500

    0.3333    0.2500    0.2000

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.

EDU>> A(2,2)

ans =

   4.0000 + 8.0000i

Section 3: Matrix Operation, Array Operations edit

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.

EDU>> [1,2,3,4].*[1,2,3,4]

ans =

     1     4     9    16

EDU>> [1,2,3,4].^2

ans =

     1     4     9    16

Section 4: Statements, Expressions and Variables; Saving a Session edit

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 edit

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