Download MATLAB: Understanding Matrices and Vector Operations and more Study notes Engineering in PDF only on Docsity! MATLAB is a software package that makes it easy to manipulate matrices. Vectors can also be easily handled as a special case. A matrix in MATLAB is just a 2-D array of numbers. Example: >> A =[1 2; 3 5; 2 4; 6 7] A = 1 2 3 5 2 4 The dimensions of a matrix is (NxM) where N is the number of rows and N is the number of columns. The matrix A above has dimensions (4x2) 6 7 We can add or subtract matrices by simply adding or subtracting each element in the arrays located at the same position. Example: >> A = [ 1 2; 3 4] A = 1 2 3 4 >> B = [3 3; 5 5] B = 3 3 5 5 C A B>> = + C = 4 5 8 9 To add matrices they must be of the same size We can consider vectors to be just examples of matrices where one of the dimensions =1. We can either have row or column vectors: >> v1 =[ 1 2 3 5] (1x4) (row) vector v1 = 1 2 3 5 >> v2 =[1;2;3;5] v2 = 1 (4x1) (column) vector 2 3 5 we can multiply vectors A and B in the same fashion as other matrices as long as we adhere to the previous rule: we must have the number of columns of A be equal to the number of rows of B in the product A*B Consider the two vectors: >> v1 =[ 1 2 3 4]; >> v2 =[1;2;3;4]; >> d=v1*v2 (1x4) (4x1) d = 30 (1x1) scalar This is just the dot product ( or the square of the magnitude in this case)!! Now consider reversing the order: >> v1 =[ 1 2 3 4]; >> v2 =[1;2;3;4]; >> p= v2*v1 (4x1) (1x4) p = 1 2 3 4 2 4 6 8 3 6 9 12 (4x4) 4 8 12 16 Here 2 1mn m np v v= If we want to get the same result for b as before but now in the form of a row vector we need to multiply x by the transpose of M, MT, where if 11 12M MM ⎡ ⎤ = ⎢ ⎥ 21 22 11 21 12 22 T M M M M M M M ⎣ ⎦ ⎡ ⎤ = ⎢ ⎥ ⎣ ⎦ (interchange rows and columns) This works since for any two matrices or vectors A, B, where the product is defined we have (AB)T =BTAT so if we take the transpose of 11 12 1 1M M x b M M x b ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ =⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦21 22 2 2 we get [ ] [ ]11 211 2 1 2 12 22 M M x x b b M M ⎡ ⎤ =⎢ ⎥ ⎣ ⎦ In MATLAB, the transpose is MT =M' >> b=x*M' b = 17 39 same b as obtained originally but now in terms of a row vector For vector analysis, MATLAB has built-in dot and cross product functions: >> v1 = [ 1 2 6]; >> v2 = [ 3 5 -2]; >> d =dot(v1, v2) d = 1 >> c =cross(v1, v2) here the vectors must be 3-D vectors i (1 3) (3 1) i di ic = -34 20 -1 .e. x or x n mens ons >> v1*v2' we saw before we could also get the dot product by calculating v1*v2T ans = 1