matlab lecture two tuesday 5 july 2005. chapter 3
TRANSCRIPT
Matrices and Vectors
Input matrix• A = [ 1 2 5; 3 9 0]
Use semicolon ";" to suppress output
Use … for line continuation
Matrices and Vectors
Vector is a special case of matrix Scalar is considered 1x1 matrix
• g = 9.81 (no [ … ] needed) X = [ ] creates a null matrix
Examples on page 51.
Matrices and Vectors
Matrix indexing• A(i,j) ith row jth column• A(m:n, k:l) specifies submatrix• A(:, j) jth column• A(i,:) ith row• A(m:k:n,:) row m, m+k,m+2k, etc.
Matrices and Vectors
Dimension• B(2,3) = 5• matrix B created large enough to
have the (2,3) entry, I.e., B is 2 x 3. The dimension is increased
dynamically
Matrices and Vectors
Matrix Manipulation• array as index, A([1 3 4],:)
reshape(…) function Transpose by .’ Hermitian conjugate by ’ Initialization
Matrices and Vectors
Appending a row or column• A = [A; u]; A = [ A u']
Deleting a row or column• A(2, :) = [ ], delete 2nd row• A(:,1) = [ ], delete 1st column
Matrices and Vectors
Utility matrices• eye(m,n), zeros(m,n), ones(m,n),
rand(m,n), diag(A)
rot90, fliplr, flipud, tril, triu
Examples on page 56
Matrices and Vectors
Creating vectors• v= initialV : increment : finalV
• e.g., a = 0:10:100
linspace(a,b,n) and logspace(a,b,b) functions
Matrices and Vectors
Matrix operations• + addition• - subtraction• * multiplication• / division• ^ exponentiation
Matrices and Vectors
Left division and right division• A / B is A B-1 (right division)
• A \ B is A-1 B (left division), in particular the solution to equation Ax = b, is x = A\b.
Matrices and Vectors
Element-by-element operation• .* element-wise multiply• ./ element-wise right division• .\ element-wise left division • .^ element-wise exponentiation• .' nonconjugated transpose
Examples on page 60.
Relational Operations
< Less than <= Less than or equal == equal > greater than >= greater than or equal ~= not equal (different from C)
Logical Operations
& logical AND | logical OR ~ logical complement (NOT) xor exclusive OR (a function)
Math Functions
sin, asin, cos, acos, tan, atan, atan2, sec, cot, sinh, exp, log, log10, sqrt, abs, angle, conj, real, imag, fix, floor, ceiling, sign, etc.
Matrix functions• expm(A), logm(A), sqrtm(A)
Character Strings
Strings are quoted by single quotes• message = 'Leave me alone'
Strings are considered as 1xn matrix.
Save and Loading Data
save • save entire workspace in the file
matlab.mat load
• load the saved file save file.mat x y z
• save the values x, y, z in file.mat
Recording a Session with "diary"
diary filename• Save the session to filename
diary off• Turn of the diary recording
Exercises
Lesson 5, exercise 3.• Write a function factorial to computer
factorial n! for any integer n.
Exercises
Lesson 5, exercise 5.• Write a function to compute the sum
of a geometric series 1 + r + r2 + r3 + … + rn for a given r and n.
Exercises on page 74, problem 1, 4, and 5
Enter matrices• A = [2 6; 3 9]• B = [1 2; 3 4]• C = [-5 5; 5 3]
Create a big matrix that has A, B, C on the diagonal