

1.1 Introduction  Theoretical Foundation
The Matlab Interface (Figure
1.1) is divided into 3 windows: The Workspace (upper left corner) lists all defined variables and mfiles (discussed later); the Command History (lower left corner) lists the ordered sequence of commands issued in this session; and the Command Window (right side) is the main source of input and output of commands, variable definitions, and errors.


Figure 1.1 Click image to enlarge, or click here to open


In the toolbar of the interface, we can designate a directory from which to read and to which to write mfiles. It is recommended to create a new directory (e.g.
matlab) in your account (the H: drive in case of the Gateway Lab), which will host any mfiles, exercises, and assignments. For simplicity, keep all Matlab related files in this directory.
Whenever starting Matlab, change the
Current Directory to this directory to access relevant files.


Figure 1.2 Click image to enlarge, or click here to open


Figure
1.3 shows a few simple arithmetic expressions that are evaluated in the Command Window: Exponentials are typed in as x^y
, square roots as sqrt(x)
. The Workspace and Command History Windows update as expressions are evaluated in the Command Window. Any variable that appears in the Workspace can be used. ans
denotes the variable for the last evaluated answer.


Figure 1.3 Click image to enlarge, or click here to open


Variable are formed by assigning letters or words to numbers or expressions, as shown in Figure
1.4. All defined variables in the Workspace can be used throughout the session.


Figure 1.4 Click image to enlarge, or click here to open


Using variables that haven't been defined returns an appropriate error.
Declarations must follow the convention of assignment where the left side of an equal sign is always a variable and the right side an arbitrary expression.


Figure 1.5 Click image to enlarge, or click here to open


Matlab handles variables by their value as opposed to symbolically. When a variable has been assigned to a value and is used in another expression, the variable acts as the value rather than a reference to the variable name. We can observe this behavior in the series of expressions in Figure
1.6. Even though c=a+b
has been declared in terms of variables a and b, when changing the value of a, c remains to be the sum of the initial a and b.


Figure 1.6 Click image to enlarge, or click here to open


The contents of the
Workspace can be cleared by issuing the command clear
. This will remove all previously defined variables.


Figure 1.7 Click image to enlarge, or click here to open


The contents of all windows can be cleared from the
Edit menu.


Figure 1.8 Click image to enlarge, or click here to open


[x:z] defines a vector of numbers starting at x and ending at z, where each element of the array is incremented by 1. E.g. [3:9]
defines the vector [3,4,5,6,7,8,9].
[x:y:z] defines a vector of numbers starting at x and ending at z, where each element of the array is incremented by y. E.g. [10:2:20]
defines the vector [10,12,14,16,18,20].
Every definition explicitely returns the defined quantity, and each expression returns a result. The output can be suppressed by ending the expression with a
; (semicolon). E.g. a=[1:1000000]
returns a vector of 1000000 elements, a quantity that we do not necessarily want to see see element by element. The expression a=[1:1000000];
will merely define the variable without returning unwanted output.


Figure 1.9 Click image to enlarge, or click here to open


Figure
1.10 pictorially shows the difference between scalars and vectors: a scalar is a single element, while a vector is a series (or a list) of elements (of scalars).
In Figure
1.9 we have declared a scalar s and a vector t.


Figure 1.10 

Having defined a vector and a scalar, we can perform arithmetic operations. Adding (multiplying, etc.) a vector by a scalar adds (multiplies, etc.) each element of the vector by the scalar. The result of
s * t
can be assigned to an output variable u as in u = s * t
. Variable u then references the resulting vector.


Figure 1.11 Click image to enlarge, or click here to open


Two samesized vectors
a and b are added/subtracted elementbyelement using the + and  operators, as is done in matrix addition and subtraction. (Vectors are merely matrices with only one row).
In order to multiply two vectors elementbyelement, the operator
* (or /) must be preceded by a period ., as shown in Figure 1.12.


Figure 1.12 Click image to enlarge, or click here to open


In order to perform any elementbyelement operation on two vectors, their lengths must match, otherwise Matlab returns an error.
Figure
1.14 shows how two differently sized vectors cannot be combined elementbyelement, because there exist ambiguities as to how an operation is defined between a scalar and a "nothing".


Figure 1.13 Click image to enlarge, or click here to open


Figure 1.14 

Besides using the range notation, vectors can also be defined by enumerating the elements one by one (Figure
1.15). Transposing can be accomplished by using a single quote (') after the variable name, e.g. b'
. Two same size vector can then be multiplied in matrixterms by issuing the command a * b'
.
Figure
1.16 pictorially shows a transpose and a double transpose operation.


Figure 1.15 Click image to enlarge, or click here to open


Figure 1.16 

Elements in a vector can be addressed individually by enclosing the elements index in parentheses after the vector variable name, e.g.
b(3)
. The vector's elements' indices start at 1 and stop at the length of the vector. Index 0 (zero) is not used, even though it is commonly done so in programming languages.
The command
length(b)
for any matrix b evaluates to the number of elements in vector b.


Figure 1.17 Click image to enlarge, or click here to open


A matrix is defined by rows and columns. A row of elements is delimited by commas, e.g.
1,2,3
. Columns are delimited by semicolons, e.g. 5;6;7
. A matrix can thus be defined using any of the vector defining notations used previously, e.g. [1,2,3;4,5,6;7,8,9]
or [[1:3];[4:6];[7:9]]
.
Figure
1.19 pictorially shows what are considered rows and columns of a matrix, indexing conventions, and the general form of indexing into a matrix.


Figure 1.18 Click image to enlarge, or click here to open


Figure 1.19 

Matrices of same dimensions can be added and subtracted as is defined in matrix arithmetic. In multiplication, however, we achieve different results depending on whether we multiply two matrices by using
a*b
or a.*b
, where the latter is elementbyelement multiplication.


Figure 1.20 Click image to enlarge, or click here to open


Random numbers, vectors, and matrices can be formed using the function
rand. rand takes as arguments the number of rows, and number of columns: rand(rows,cols) and returns a matrix of that size with random numbers ranging from 0 to 1. The function can easily be multiplied and/or added to scalars in order to produce random numbers in any range, e.g. a random number between 0 and 10: rand(1,1) * 10
, or a random number between 10 and 100: (rand(1,1) * 90) + 10
.


Figure 1.21 Click image to enlarge, or click here to open


Matrices and vectors can be appended to form larger matrices and vectors. Given two similarly or differently sized vectors
a and b, a vector c can be formed by c=[a,b]
. c is now as long as a and b combined. The same holds for matrices. A 3x3 matrix a can be combined with a 3x5 matrix b by [a,b]
but not by [a;b]
, while a 3x3 matrix a can be combined with a 5x3 matrix b by [a;b]
but not by [a,b]
. Depending on which way matrices are appended, one of the dimensions (rows or columns) must match.


Figure 1.22 Click image to enlarge, or click here to open


Functions
ceil(x) and floor(x) round decimal numbers (doubles) up and down to the next integer, respectively. This operation can be performed on scalars, vectors, and matrices.


Figure 1.23 Click image to enlarge, or click here to open


Elements in a matrix can be accessed by rows and columns:
A(r,c), where A is a matrix, r is a row, and c is a column.


Figure 1.24 Click image to enlarge, or click here to open


Rows and columns can also be accessed. To access a row in matrix
A, a colon (:) is substituted for the column, and vice versa: row_r = A(r,:)
, column_c = A(:,c)
.
The concept of indexing into a matrix by row and column, only row, and only column is pictorially described in Figure
1.26.


Figure 1.25 Click image to enlarge, or click here to open


Figure 1.26 

It is also possible to extract portions of a matrix by using ranges such as
[1,2] or [1:3] as indices, as shown in Figure 1.27.
The concept of extracting submatrices from matrices is shown in Figure
1.28.


Figure 1.27 Click image to enlarge, or click here to open


Figure 1.28 

Not only can we extract a submatrix, but we can also replace portions of a matrix. The same form of indexing is used, as shown in Figure
1.29.
The concept of replacing submatrices is pictorially described in Figure
1.30.


Figure 1.29 Click image to enlarge, or click here to open


Figure 1.30 

To get quick help on any Matlab function, we type
help function
where function is the name of the function. help rand
, for example, gives detailed help on function rand.


Figure 1.31 Click image to enlarge, or click here to open


To save a collection of expressions, for example for inclass exercises and homework assignments, you will need to use mfiles. Mfiles are no more than simple text files that can be executed from within Matlab to reproduce the statements in the mfile.
To create a new mfile, go to
File > New > Mfile, as shown in Figure 1.32.


Figure 1.32 Click image to enlarge, or click here to open


We can now enter a number of statements, one statement per line. In a later lecture, we will see how to create functions using mfiles.


Figure 1.33 Click image to enlarge, or click here to open


Save the mfile in the
matlab directory which you have earlier chosen as the Current Directory. Make sure that the extension of this file is .m, i.e. make sure that the file ends with .m


Figure 1.34 Click image to enlarge, or click here to open


You can now execute the statements in the mfile by typing in the name of the mfile in the
Command Window. Make sure to omit the .m extension.


Figure 1.35 Click image to enlarge, or click here to open



