Download Matrices in Matlab

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

System of linear equations wikipedia , lookup

Linear least squares (mathematics) wikipedia , lookup

Rotation matrix wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Principal component analysis wikipedia , lookup

Jordan normal form wikipedia , lookup

Determinant wikipedia , lookup

Four-vector wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Orthogonal matrix wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Gaussian elimination wikipedia , lookup

Matrix calculus wikipedia , lookup

Matrix multiplication wikipedia , lookup

Transcript
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Computing for Engineers and Scientists CITS1005
Arrays and Vectors
Topic 6
• An array is a collection of data objects of the same type, typically
stored sequentially in memory.
Matrices in Matlab
• An array is an elementary data structure.
• Almost all programming languages provide support for arrays.
• Matlab is a language that has been particularly specialised to support
arrays (and subsequently matrices).
• An array is the obvious way to represent a vector.
• A 3D vector with coordinates [1, 2.5, 5] would be represented as an
array of 3 doubles arranged sequentially in memory.
1 2.5 5
Matlab®
Computing for Engineers and Scientists CITS1005
Declaring (Constructing) Arrays
• In Matlab, we can construct an array and associate it with an identifier
very easily. For example:
>> a = [1, 2.5, 5]
a =
1.0000
2.5000
5.0000
• The commas are optional and can be omitted:
>> a = [1 2.5 5]
a =
1.0000
2.5000
5.0000
• The square brackets indicate to Matlab that the contents represent an
array:
Matlab®
Computing for Engineers and Scientists CITS1005
Operations on Arrays
• This process of constructing an array involves a segment of
memory being allocated and associated with the variable
name, and the elements of memory being set to the specified
values.
• In most programming languages you would have to declare
an array and assign the values one at a time. In Matlab this is
automatic.
• You can then perform arithmetic on arrays as simply as you
can with scalars. For example:
>> whos
Name
Size
Bytes Class
a
1x3
24 double array
Grand total is 3 elements using 24 bytes
>> b = a*2
b =
2
5
10
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Working with Array Elements
• You can extract individual values from an array by specifying the index
within the array using round brackets. For example:
>>c =
b(1)
% c is set to the value of
the first element of b
c =
2
• You can also assign new values to individual elements of an array. For
example:
>> b(3) = 6
b =
2
5
% Set the value of the 3rd element of b to 6
6
• In Matlab, the index of the first element of an array is always 1.
• Note: this differs from languages such a C or Java where the index of the
first element is always 0.
Matlab®
Computing for Engineers and Scientists CITS1005
Matrices
• An array is a collection of data objects of the same type.
• 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:
>> a = [1 2 3; 4 5 6]
% The ; separates the
% individual 1D arrays.
Computing for Engineers and Scientists CITS1005
Size of an Array
• Matlab keeps track of the size of arrays and ensures
you do not try to go beyond their bounds. For
example:
>> b(4)
??? Index exceeds matrix dimensions.
>> b(0)
! ??? Index into matrix is negative or zero.
Matlab®
Computing for Engineers and Scientists CITS1005
Everything in Matlab is a Matrix
• 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:
>> a = [1 2 3
4 5 6 ];
% A matrix consisting of two rows
!
a =
1
4
2
5
3
6
• As far as Matlab is concerned, everything is a matrix!
• A vector is a 1xN (or Nx1) matrix; a scalar is a 1x1 matrix.
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Matrix and vector operators
•
The standard mathematical operators can be applied to vectors and matrices.
Matlab handles all the details automatically.
•
For example, suppose we have two matrices defined as:
>> a = [1 2
3 4];
% Initialise two matrices.
Addition and subtraction
• Matrix addition and subtraction is same as linear algebra. For
example:
>> c = a + b
6
10
•
The transpose operator switches the rows and columns of a matrix.
•
The transpose operator is denoted with the single apostrophe (') symbol.
>> a'
8
12
>> c = a + 2
% Addition of a scalar results
% in the scalar being added to
% the matrix elements.
c =
3
5
% The transpose rows and
% columns.
ans =
•
% Matrix addition (and
subtraction).
c =
>> b = [5 6
7 8];
1
2
Computing for Engineers and Scientists CITS1005
4
6
• For matrix addition or subtraction to work, the dimensions of
the two matrices must match.
3
4
Transpose has higher precedence than multiplication.
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Matrix multiplication
• Matrix multiplication is defined as in standard linear algebra. For
example:
>> c = a * b
c =
19
22
43
50
% Matrix multiplication.
>> c = a * 2
%
%
%
%
Matlab
scalar
Matlab
scalar
recognises this as
multiplication.
also recognises
division.
c =
Computing for Engineers and Scientists CITS1005
Point-wise Multiplication
• 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 (sometimes
called “array multiplication”).
• For example:
>> c = a .* b
2
6
4
8
• For matrix multiplication to work, the number of columns in the first
matrix must match the number of rows in the second matrix.
c =
5
21
12
32
% Point-wise multiplication.
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Computing for Engineers and Scientists CITS1005
Matrix Division
Point-wise Division and Exponentiation
•
The ./ operator performs point-wise division on each corresponding pair of
elements of two matrices.
•
For example:
>> c = a ./ b
% Point-wise division.
•
Matrix division implies solving for matrix inverses. Matlab handles this automatically!
•
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:
>> c = a / b;
c =
0.2000
0.4286
0.3333
0.5000
c =
•
The .^ operator performs point-wise exponentiation on each corresponding pair of
elements of two matrices.
•
For example:
3
2
•
>> c = a .^ b
Left division is pre-multiplication by the inverse of a matrix:
% Point-wise exponentiation.
-3
4
64
65536
Computing for Engineers and Scientists CITS1005
Checking...
• Left division is the most common.
• The expression c = a\b above would solve the equation b = a*c.
Matlab®
-4
5
Computing for Engineers and Scientists CITS1005
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.
• Matlab provides the colon operator (:) for constructing
sequences of values.
• Double check:
>> a*c
ans =
5
7
% c = a-1 * b
c =
c =
Matlab®
-2
-1
>> c = a \ b
1
2187
% c = a * b-1
6
8
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.
• 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:
array = first : increment : last
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Computing for Engineers and Scientists CITS1005
Colon Operator
•
Subarrays
For example:
• As well as selecting individual elements from arrays, Matlab
allows for the selection of sections of an array.
>> x = 3 : 2 : 11
• For example:
x =
3
5
7
9
11
>> a = [10:-1:1]
•
If the increment is 1, it can be omitted.
•
For example:
a =
10
9
8
7
6
5
4
3
2
1
>> x = 1 : 10
x =
1
•
2
3
4
5
6
7
8
9
>> a(4:9)
10
The colon operator is enormously useful, not only for array creation but also for loop
control.
% Use the colon operator (with a
% default increment of 1) to select
% elements 4 to 9 from the array.
ans =
7
Matlab®
Computing for Engineers and Scientists CITS1005
Subarrays (cont.)
Matlab®
6
5
4
3
2
Computing for Engineers and Scientists CITS1005
Extracting Data from 2D Arrays
Matrix selection operations are extended to 2D arrays in a natural way.
• You can also assign values to subarrays.
>> a = [1 2 3
4 5 6
7 8 9]
>> a(1:3) = [8 9 10]
% Define a 2D array.
a =
8
9
10
7
6
5
4
3
2
1
• The size of the array being assigned must match the
size of the array selected.
>> a(2, 3)
% Get the value at row 2, column 3.
ans =
6
>> a(2,1:3)
ans =
4
% Get row 2, columns 1 to 3.
5
>> a(2:3, 1:2)
ans =
4
7
5
8
6
% Get the values from rows
% 2-3 in columns 1-2.
Matlab®
Computing for Engineers and Scientists CITS1005
Matlab®
Computing for Engineers and Scientists CITS1005
Extracting all rows or columns
• 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:
>> a(:, 2)
% Get the values from every
% row in column 2.
% Same as a(1:3, 2)
ans =
• 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]
>> b = [a 7 8]
% Concatenate 7 and 8 onto the
% end of a.
b =
2
5
8
1
>> a(2:3, :)
% Get the values from rows
% 2-3 in all columns.
ans =
4
7
Matrix concatenation
5
8
6
9
2
>> a = [a a(1:2)
b ]
3
7
%
%
%
%
%
8
Construct a new matrix a. The
first row is the "old a" with
elements 1:2 of a concatenated
to the end. The second row
is made from array b.
a =
1
1
2
2
3
3
1
7
2
8
22
Matlab®
Computing for Engineers and Scientists CITS1005
Assigning to arrays and subarrays
• 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.
• Matlab handles assignment to subarrays and arrays
differently.
– For assignment statements involving subarrays, the shapes
of the subarrays on either side of the equal sign must
match. Otherwise, Matlab will produce an error. The
assignment will only replace the specified elements.
– In contrast, assigning to an existing array will replace the
entire contents of the array, may even resize the array.
Matlab®
Computing for Engineers and Scientists CITS1005
Summary of Arithmetic Operations
24
Matlab®
Computing for Engineers and Scientists CITS1005
Arithmetic Operations between Two Scalars
Operation
Algebraic Form
Matlab Form
Addition
a+b
Subtraction
a-b
Multiplication
a*b
Division
a/b
Exponentiation
a^b
Matlab®
Computing for Engineers and Scientists (CITS1005)
Common “Array” and Matrix Operations (2)
Operation
Array Right
Division
Array Left
Division
Matlab
Form
Comments
a ./ b
Element-by-element division of
a and b: a(i,j)/b(i,j). Both arrays
must of the same shape, or one
of them must be a scalar.
a .\ b
Element-by-element division of
a and b, but b in the numerator
b(i,j)/a(i,j). Both arrays must of
the same shape, or one of them
must be a scalar.
Matlab®
Computing for Engineers and Scientists CITS1005
Common “Array” and Matrix Operations (1)
Operation
Matlab
Form
Comments
Addition
a+b
Array addition and Matrix addition
are identical
Subtraction
a-b
Array subtraction and matrix
subtraction are identical
Array
Multiplication
a .* b
Element-by-element multiplication
of a and b. Both arrays must of the
same shape, or one of them must be
a scalar
Matrix
Multiplication
a*b
Matrix multiplication of a and b.
The number of columns in a must
be equal to number of rows in b.
Matlab®
26
Computing for Engineers and Scientists (CITS1005)
Common “Array” and Matrix Operations (1)
Operation
Matlab
Form
Comments
Matrix Right
Division
a/b
Matrix division is defined by
a*inv(b), where inv(b) is the inverse
of matrix b.
Matrix Left
Division
a\b
Matrix division is defined by
inv(a)*b, where inv(a) is the inverse
of matrix a.
a .^ b
Element-by-element exponentiation
of a and b: a(i,j)^b(i,j). Both arrays
must of the same shape, or one of
them must be a scalar.
Array
Exponentiation