Practical 1 – AMAT181
Matrices in MatLab
Entering Matrices
Example:
> A = [1 2 3; 7 8 9]
A =
1 2 3
7 8 9
Use ‘ ; ’ to indicate the end of each row
Use space (or comma) to separate elements of a row
Copy and paste the MatLab output after you type the following commands:
Practice Examples:
- A = [5 7 2; 1 3 0]
- B = [4 3 -1; 1 0 1] B = [4 3 -1; 1 0 1]
- a = [4 3; 1 0; 8 1/2; -2 pi] % pi is the number 3.14
- D = [0.5; 1;3] % column vector
- d = [123456789] % row vector
- S = [1 2; 5 7]
Matrix operations
- + addition
- - subtraction
- * multiplication
- ^ power
- ‘ transpose
- / division
- To make the ‘*’ , ‘^’, and ‘/’ entry-wise, we precede the operators by ‘.’
Practice Examples:
7. A+B
8. B-A
9. S * A
10. A * S
11. A .* B
12. S^2
12. S .^2
13. A’
14. K= A*B'
Matrix Build-in Functions
- Use the zeros function to create a Null matrix.
- Use the ones function to create a matrix with ones.
- Use the eye function to create the Identity matrix.
- Use the diag function to find the diagonal of a matrix, or to create a diagonal matrix.
- Use the det function to find the determinant of a matrix.
- Use the inv function to find the inverse of a matrix
To get help for each function in MATLAB use the command
help <function name>.
Students Name:Exercise 1: Find the output of the following commands, copy and paste it under the command in the table:
help zeroszeros (2,4) / zeros(4)
help(ones)
ones (1,5) / ones(2)
help eye
eye (3) / eye (5,3)
help det
det ([1 2; 2 7]) / det (A) % note that A is a 2x3 matrix
help diag
diag ([1 2 5; 2 4 7]) / diag ([1 2 3 4])
help inv
inv ([1 2; 2 7]) / inv(eye(3)) % inverse of an identity matrix
help inv
inv ([1 2; 2 7]) / inv(eye(3)) % inverse of an identity matrix
Exercise 2: Copy and paste the output to the answer of each question.
a)Create the row matrix R containing the numbers -2 3 4.
b)Create the column matrix C with the same numbers.
c)Create a 3x3 Ones matrix. (use the function ones).
d)Create a 2x3 Null matrix. (use the function zeros).
e)Create a 3x3 Identity matrix. (use the function eye).
Exercise 3: Copy and paste the output to the answer of each question.
a)Enter the following matrix
b)Find the diagonal of A. (using the function)
c)What is the order of A x B?
d)Find the determinant of A.
e)Find the inverse of A.
Page 1 of 4