| Home > Undergraduate > Computing for Engineers and Scientists > Lectures > Lecture 5 |
the = sign in this code does not mean equality in the mathematical sense
(clearly it cannot).
The = symbol really means the operation of assignment.
We can read the expression a = a*3 as:
The value of 2 stored at the memory location referred to by b remains unchanged.
Pseudo-code often uses the symbol <- (or :=) to denote assignment rather than the = symbol, so that the distinction between the two is made clear.
Testing for equality between two values is done with a different operator.
Instead, we can create a script file - a file that contains a sequence of commands (a script) for Matlab to follow.
A script file is simply a text file that contains the sequence of Matlab commands/expressions to follow.
Script files can be created using any text editor (e.g. SimpleText on the Macintosh) or by using the Matlab internal editor.
By convention, script files should be saved with a .m ending.
Script files are executed by typing the name of the script without the .m ending.
The commands in the script file are executed as if they had been typed in at the command prompt.
For example:
The ; acts as a separator between expressions.
If you want the result of each expression in the line to be printed to the screen, commas (,) are used as statement separators.
For example:
You can break an expression over more than one line using ... to indicate a continuation.
For example:
In general, seek to format your expressions to aid readability and understanding.
The data objects of the array can themselves be arrays.
A matrix is typically represented by an array of arrays, or a 2D array.
Matlab supports matrices in the same way that it supports vectors.
Matlab uses the semi-colon (;) operator to distinguish between the different rows of a matrix.
For example:
Matlab also allows rows to be entered on different lines.
Once an array is started by a square bracket ([), Matlab assumes that a new line means a new row of the matrix.
For example:
As far as Matlab is concerned, everything is a matrix.
A vector is a 1xN (or Nx1) matrix; a scalar is a 1x1 matrix.
For example, imagine we have two matrices defined as:
The transpose operator switches the rows and columns of a matrix.
The transpose operator is denoted with the single apostrophe (') symbol.
For example:
Transpose has higher precedence than multiplication.
Matrix addition and subtraction is done point-wise on each corresponding pair of elements.
For example:
For matrix addition or subtraction to work, the dimensions of the two matrices must match.
Matrix multiplication is defined as in standard linear algebra.
For example:
For matrix multiplication to work, the number of columns in the first matrix must match the number of rows in the second matrix.
Associated with matrices and vectors are a number of special operators, many of which are unique to Matlab.
The .* operator performs point-wise multiplication on each corresponding pair of elements of two matrices.
For example:
The ./ operator performs point-wise division on each corresponding pair of elements of two matrices.
For example:
The .^ operator performs point-wise exponentiation on each corresponding pair of elements of two matrices.
For example:
Note that because matrix multiplication is not commutative, we require the concept of left and right division.
Right division is post-multiplication by the inverse of a matrix:
Left division is pre-multiplication by the inverse of a matrix:
Left division is the most common.
The expression c = a\b above would solve the equation b = a*c.
Double check:
which is the value of the b matrix.
Most of these expressions would require at least 5 lines of code if programmed in some other language such as C or Java.
Matlab's syntax yields very concise and readable code.
Matlab provides the colon operator (:) for constructing sequences of values.
The colon operator produces an array equivalent to the elements of an arithmetic sequence.
Arithmetic sequences are defined in terms of the first value in the series, the increment between successive values, and the last value in the series.
The syntax for building an array using the colon operator is:
For example:
If the increment is 1, it can be omitted.
For example:
The colon operator is enormously useful, not only for array creation but also for loop control.
For example:
You can also assign values to subarrays.
The size of the array being assigned must match the size of the array selected.
Matrix selection operations are extended to 2D arrays in a natural way.
If you want to extract all the rows (or all the columns) from a matrix, you can use the empty colon operator to specify the row or column.
For example:
For example:
Note that in the last example, the memory required to store the new matrix will not "fit into" the old space occupied by the original matrix.
Matlab will handle any memory allocation needed to make matrices fit.
The distinction between assignment and equality
When we write:
>> a = 2;
>> b = a;
>> a = a*3;
Script files
Matlab allows commands to be entered interactively at the command prompt, but this is not really appropriate for extended sequences of commands.
Formatting expressions on lines
You can put several expressions on one line with the semi-colon (;) operator.
>> a = 'B'; b = 1; c = 1.5;
>> a = 'B', b = 1, c = 1.5
a =
B
b =
1
c =
1.5000
>> a = 1 ...
+ 2 ...
+ 3;
Matrices in Matlab
An array is a collection of identical data objects.
>> a = [1 2 3; 4 5 6] % The ; separates the individual 1D arrays.
a =
1 2 3
4 5 6
>> a = [1 2 3 % A matrix consisting of two rows.
4 5 6];
Matrix and vector operators
The standard mathematical operators can be applied to vectors and matrices.
Matlab handles all the details automatically.
>> a = [1 2 % Initialise two matrices.
3 4];
>> b = [5 6
7 8];
>> a' % The transpose operator.
ans =
1 3
2 4
>> c = a + b % Matrix addition (and subtraction).
c =
6 8
10 12
>> c = a + 2 % Addition of a scalar results in the scalar
% being added to all the matrix elements.
c =
3 4
5 6
>> c = a * b % Matrix multiplication.
c =
19 22
43 50
>> c = a * 2 % Matlab recognises this as scalar
% multiplication.
% Matlab also recognises scalar division.
c =
2 4
6 8
>> c = a .* b % Point-wise multiplication.
c =
5 12
21 32
>> c = a ./ b % Point-wise division.
c =
0.2000 0.3333
0.4286 0.5000
>> c = a .^ b % Point-wise exponentiation.
c =
1 64
2187 65536
Matrix division
Matrix division implies solving for matrix inverses.
Matlab handles this automatically.
>> c = a / b; % c = a * b-1
c =
3 -2
2 -1
>> c = a \ b % c = a-1 * b
c =
-3 -4
4 5
>> a*c
ans =
5 6
7 8
Array constructors - the colon operator
It is easy to construct small arrays by explicitly specifying all the elements, but this is not practical for large arrays.
array = first : increment : last
>> x = 3 : 2 : 11
x =
3 5 7 9 11
>> x = 1 : 10
x =
1 2 3 4 5 6 7 8 9 10
Subarrays
As well as selecting individual elements from arrays, Matlab allows for the selection of sections of an array.
>> a = [10 9 8 7 6 5 4 3 2 1]
>> a(4:9) % Use the colon operator (with a default
% increment % of 1) to select elements
% 4 to 9 from the array.
ans =
7 6 5 4 3 2
>> a(1:3) = [8 9 10]
a =
8 9 10 7 6 5 4 3 2 1
>> a = [1 2 3 % Define a 2D array.
4 5 6
7 8 9]
a =
1 2 3
4 5 6
7 8 9
>> a(2, 3) % Get the value at row 2, column 3.
ans =
6
>> a(2:3, 1:2) % Get the values from rows 2-3 in columns 1-2.
ans =
4 5
7 8
>> a(2:3, :) % Get the values from rows 2-3 in all columns.
ans =
4 5 6
7 8 9
>> a(:, 2) % Get the values from every row in column 2.
ans =
2
5
8
Matrix concatenation
Matlab has a very convenient syntax for concatenating matrices - just stick the matrices side by side, or on top of each other, within a set of enclosing square brackets.
>> a = [1 2 3]
a =
1 2 3
>> b = [a 7 8] % Concatenate 7 and 8 onto the end of a.
b =
1 2 3 7 8
>> a = [a a(1:2) % Construct a new matrix a.
b ] % The first row is the "old a" with
% elements 1:2 of a concatenated to the end.
a = % The second row is made from the array b.
1 2 3 1 2
1 2 3 7 8
Copyright © 2000 - 2005
School of Computer Science & Software Engineering
The University of Western Australia
Last modified: 2003-07-25 16:06:54.000000000 +0800