Matlab Training Session 2: Matrix Operations and Relational Operators
Matlab Training Session 2: Matrix Operations and Relational Operators
Course Website:
http://www.queensu.ca/neurosci/matlab.php
Week 2 Lecture Outline
Fundamentals of Matlab 1
A. Week 1 Review
B. Matrix Operations:
The empty matrix
Creating multi-dimensional matrices
Manipulating Matrices
Matrix Operations
C. Operators
Relational Operators
Week 1 Review
Working with Matrices
>> A(2,1) = 9
Ans
A = [1 2 4 5
9 3 8 2]
Week 1 Review
Indexing Matrices
A = [1 2 4 5
6 3 8 2]
The colon operator can be used to index a range of elements
>> A(1,1:3)
Ans 1 2 4
Matrix Indexing Cont..
Indexing Matrices
A = [1 2 4 5
6 3 8 2]
The colon operator can index all rows or columns without setting
an explicit range
>> A(:,3)
Ans 4 8
>> A(2,:)
Ans 6 3 8 2
B. Matrix Operations
Matrix Operations
Indexing Matrices
An empty or null matrix can be created using square brackets
>> A = [ ]
** TIP: The size and length functions can quickly return the number
of elements and dimensions of a matrix variable
Matrix Operations
Indexing Matrices
A = [1 2 4 5
6 3 8 2]
>> A(:,3) = [ ]
A = [1 2 5
6 3 2]
>> A(2,:) = [ ]
A = [1 2 5]
Matrix Operations
Indexing Matrices
A = [1 2 4 5
6 3 8 2]
>> A(1,3) = [ ]
??? Subscripted assignment dimension mismatch.
N Dimensional Matrices
A = [1 2 4 5 B = [5 3 7 9
6 3 8 2] 1 9 9 8]
>> C = cat(3,[1,2,4,5;6,3,8,2],[5,3,7,9;1,9,9,8])
>> C = cat(3,A,B)
Matrix Operations
Scalar Operations
Scalar (single value) calculations can be can performed on
matrices and arrays
A = [1 2 4 5 B = [1 C = 5
6 3 8 2] 7
3
3]
Try:
A + 10; A * 5; B / 2; A.^C; A*B
Matrix Operations
Scalar Operations
Scalar (single value) calculations can be performed on matrices
and arrays
A = [1 2 4 5 B = [1 C = 5
6 3 8 2] 7
3
3]
Try:
A + 10
A*5
B/2
A^C What is happening here?
Matrix Operations
Matrix Operations
Matrix to matrix calculations can be performed on matrices and
arrays
Addition and Subtraction
Matrix dimensions must be the same or the added/subtracted
value must be scalar
A = [1 2 4 5 B = [1 C = 5 D = [2 4 6 8
6 3 8 2] 7 1 3 5 7]
3
3]
Try:
>>A + B >>A + C >>A + D
Matrix Operations
Matrix Multiplication
The dot product for two matrices A and B is defined whenever the
number of columns of A are equal to the number of rows of b
A(x1,y1) * B(x2,y2)
Matrix Operations
The Dot Product
The dot product for two matrices A and B is defined whenever the
number of columns of A are equal to the number of rows of b
A(x1,y1) * B(x2,y2)
Matrix Operations
The Dot Product
The dot product for two matrices A and B is defined whenever the
number of columns of A are equal to the number of rows of b
A(x1,y1) * B(x2,y2)
Matrix Operations
The Dot Product
The dot product for two matrices A and B is defined whenever the
number of columns of A are equal to the number of rows of b
A = [1 2 B = [1 D = [2 2 E = [2 4 3 6]
6 3] 7 2 2]
3
3]
Try:
>>A * D
>>B * E
>>A * B
Matrix Operations
Element by Element Multiplication
Element by element multiplication of matrices is performed with
the .* operator
Matrices must have identical dimensions
A = [1 2 B = [1 D = [2 2 E = [2 4 3 6]
63] 7 22]
3
3]
>>A .* D
Ans = [ 2 4
12 6]
Matrix Operations
Matrix Division
A = [1 2 4 5 B = [1 D = [2 2 2 2 E = [2 4 3 6]
6 3 8 2] 7 2 2 2 2]
3
3]
>>A ./ D
Examples:
A^2 = A * A (Matrix must be square)
A.^2 = A .* A
Matrix Operations
Shortcut: Transposing Matrices
The transpose of a matrix is the matrix formed by interchanging
the rows and columns of a given matrix
A = [1 2 4 5 B = [1
6 3 8 2] 7
3
3]
>> transpose(A) >> B
A = [1 6 B = [1 7 3 3]
23
48
5 2]
Matrix Operations
Other handy built in matrix functions Include:
Relational Operators
< less than
<= less than or equal to
> Greater than
>= Greater than or equal to
== equal
~= not equal
Relational Operators
Comparison occurs between pairs of corresponding elements
A 1 or 0 is returned for each comparison indicating TRUE or
FALSE
Matrix dimensions must be equal!
>> 5 == 5
Ans 1
>> 20 >= 15
Ans 1
Relational Operators
A = [1 2 4 5 B=7 C = [2 2 2 2
6 3 8 2] 2 2 2 2]
Try:
>>A > B
>> A < C
Relational Operators
The Find Function
The find function is extremely helpful with relational operators
for finding all matrix elements that fit some criteria
A = [1 2 4 5 B=7 C = [2 2 2 2 D = [0 2 0 5 0 2]
6 3 8 2] 2 2 2 2]
The positions of all elements that fit the given criteria are
returned
A = [1 2 4 5 B=7 C = [2 2 2 2 D = [0 2 0 5 0 2]
6 3 8 2] 2 2 2 2]
The find function can also return the row and column indexes of
of matching elements by specifying row and column arguments
>> [x,y] = find(A == 5)
>> A(x,y) = 10
A = [ 1 2 4 10
6382 ]
Getting Help
Help and Documentation
Digital
1. Accessible Help from the Matlab Start Menu
2. Updated online help from the Matlab Mathworks website:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html
3. Matlab command prompt function lookup
4. Built in Demos
5. Websites
Hard Copy
3. Books, Guides, Reference
The Student Edition of Matlab pub. Mathworks Inc.