Presentation is loading. Please wait.

Presentation is loading. Please wait.

(The Transpose Operator) 1 >> C=[2 55 14 8; 21 5 32 11; 41 64 9 1] C = 2 55 14 8 21 5 32 11 41 64 9 1 >> D=C' D = 2 21 41 55 5 64 14 32 9 8 11 1.

Similar presentations


Presentation on theme: "(The Transpose Operator) 1 >> C=[2 55 14 8; 21 5 32 11; 41 64 9 1] C = 2 55 14 8 21 5 32 11 41 64 9 1 >> D=C' D = 2 21 41 55 5 64 14 32 9 8 11 1."— Presentation transcript:

1 (The Transpose Operator) 1 >> C=[2 55 14 8; 21 5 32 11; 41 64 9 1] C = 2 55 14 8 21 5 32 11 41 64 9 1 >> D=C' D = 2 21 41 55 5 64 14 32 9 8 11 1

2 2  Addition or subtraction of matrices is possible as long as they are the same size. >> [1 2 3]+[4 5 6] ans = 5 7 9 To deal with arrays on an element-by-element level we need to use the following array or dot-operators:.*,./ and.^  Element-by-element multiplication and division uses the.* and./ operators. The * and / operators are used for complete matrix multiplication and division. >> [1 2 3].*[4 5 6] ans = 4 10 18 (Vectors Arithmetic 2)

3 dot-operators Because scalars are equivalent to a 1×1 array, you can either use the standard or the dot-operators when doing multiplication, division and exponentiation of scalars (i.e., of single numbers). It is okay for you to always use the dot-operators, unless you intend to perform vector or matrix multiplication or division..*,./ and.^

4 Array vs. Matrix Operations Example: m=[2,1;3,4] p=[5,6;7,8] (in class we had a look on m=[1,2;3,4] and results were different ) q=m.*p results in [10, 6; 21, 32]; this is array multiplication q = m * p results in [17, 20; 43, 50]; this is matrix multiplication So, do NOT forget the dot when doing array operations! (.*./.^)

5 (More about Column Vectors) 5  The vectors seen so far are row vectors (horizontal). You can define column vectors as well. For row vectors you separate the elements with spaces or commas; for column vectors you use semi-colons. >> colvec = [1;2;3] colvec = 1 2 3

6 (More about Matrices) 6  You can define matrices the same way by combining rows and columns. >> mat1 = [1 2;3 4] mat 1 = 1 2 3 4 >> mat2 = [mat1;mat1] mat2 = 1 2 3 4 1 2 3 4

7 (The.^ and sqrt Commands) 7 .^ us used for the element-by-element power operator (works like.* and./ seen in slide 25). >> [1 2 3].^2 ans = 1 4 9  Square roots are done with sqrt (works with negative numbers too!). >> sqrt (-9) ans = 0 + 3.0000i >> sqrt ([1 2 4 9]) ans = 1.0000 1.4142 2.0000 3.0000

8 (Trig Commands) 8  Trig functions are supported like sin. >> sin ([pi/4 pi/2 pi]) ans = 0.7071 1.0000 0.0000  A few more command examples... There are many many more!  log(x), log10(x), cos(x), atan(x), exp(x), round(x), floor(x), ceil(x), angle(x), abs(x)...  They work exactly like the math library functions in C. Need help with the sin function? Try help sin or doc sin.

9 (Saving and Loading Variables) 9  Use save to save variables to a file. >> save myfile mat1 mat2 saves matrices mat1 and mat2 to the file myfile.mat  To load saved variables back into the workspace, use load. >> load myfile  If the command window is too full, use clc.

10 (More about Transpose) 10  The transpose operators turn a column vector into a row vector and vice versa as seen before. For example to transpose a vector x, >> transpose (x); or >> x'  You can also use x.' if you want to transpose a vector containing complex numbers.

11 (Example: Scalar Product) 11  A scalar product can be done with one row vector by a column vector. A transpose may be required. >> scalarprod = [1 2 3 4] * [4 5 6 7]' scalarprod = 60

12 (Multiple Initializations) 12  It is possible to perform multiple initializations at once: >> v1 = ones (1,10) v1 = 1 1 1 1 1 1 1 1 1 1 >> v1 = rand (1,100); A row vector with 100 random elements between 0 and 1. >> v1 = zeros (45,1); A column vector with 45 zero elements.

13 (Index vs. Subscript) 13  For a vector, indexes and subscripts are the same. Remember that indexes and subscripts start at 1 in MATLAB.  For matrices, subscripts have the row and column numbers separated by a comma. Indexes, on the other hand, indicate the linear position from the start of the matrix. subscripts indexes

14 (Matrix Indexing) 14  The index argument can be a matrix. In this case, each element is looked up individually, and returned as a matrix of the same size as the index matrix. >> a = [10 20 30 40]; >> b = a([1 2 4;3 4 2]) b = 10 20 40 30 40 20

15 (Working Whole Rows or Columns) 15  The colon operator selects rows or columns of a matrix. >> c = b (1,:) c = 10 20 40 >> d = b (:,2) d = 20 40 >> b (:, 3) = [100 200] b = 10 20 100 30 40 200

16 The colon : lets you address a range of elements Vector (row or column) va(:) - all elements va(m:n) - elements m through n Matrix A(:,n) - all rows of column n A(m,:) - all columns of row m A(:,m:n) - all rows of columns m through n A(m:n,:) - all columns of rows m through n A(m:n,p:q) - columns p through q of rows m through n (Colon Operator) 16

17 17

18 The colon : lets you address a range of elements Vector (row or column) va(:) - all elements va(m:n) - elements m through n Matrix A(:,n) - all rows of column n A(m,:) - all columns of row m A(:,m:n) - all rows of columns m through n A(m:n,:) - all columns of rows m through n A(m:n,p:q) - columns p through q of rows m through n (Colon Operator) 18

19 Colon Operator 19 Colon notation can be used to define evenly spaced vectors in the form: first : last mm=1:3 mm= 1 2 3 The default spacing is 1. To use a different increment use the form: first : increment : last y=1:2:7 y= 1 3 5 7 The numbers now increment by 2

20 Extracting Data 20 The colon represents an entire row or column when used in as an array index in place of a particular number. n(:,1) ans = 2 1 0 n = 2 50 20 2 50 20 1 1 2 1 1 2 0 55 66 0 55 66 n(2,:) ans = 1 1 2 The colon operator can also be used to extract a range of rows or columns: n(2:3,:) ans = 1 1 2 0 55 66 n(1,2:3) ans = 50 20

21 (Finding Minimum and Maximum Values) 21 >> vec = [10 7 8 13 11 29];  To get the minimum value and its index: >> [minVal,minInd] = min(vec) minVal = 7 minInd = 2  To get the maximum value and its index: >> [maxVal,maxInd] = max(vec) maxVal = 29 maxInd = 6 >> [a,b] = max(vec) a = 29 b = 6

22  You can replace vector index or matrix indices by vectors in order to pick out specific elements. For example, for vector v and matrix m v([a b c:d]) returns elements a, b, and c through d m([a b],[c:d e]) returns columns c through d and column e of rows a and b (Getting Specific Columns) 22

23 >> v=4:3:34 v = 4 7 10 13 16 19 22 25 28 31 34 >> u=v([3, 5, 7:10]) u = 10 16 22 25 28 31 >> u=v([3 5 7:10]) u = 10 16 22 25 28 31 (Colon Operator Examples 1) 23

24 >> A=[10:-1:4; ones(1,7); 2:2:14; zeros(1,7)] A = 10 9 8 7 6 5 4 1 1 1 1 1 1 1 2 4 6 8 10 12 14 0 0 0 0 0 0 0 >> B=A([1 3],[1 3 5:7]) B = 10 8 6 5 4 2 6 10 12 14 (Colon Operator Examples 2) 24

25 Example? 25 A = 10 9 8 7 6 5 4 1 1 1 1 1 1 1 2 4 6 8 10 12 14 0 0 0 0 0 0 0 B=A([1 3]) B = 10 2 B=A([1 3],:) B = 10 9 8 7 6 5 4 2 4 6 8 10 12 14 B=A(:,[1 3 5:7]) B = 10 8 6 5 4 1 1 1 1 1 2 6 10 12 14 0 0 0 0 0 B=A([1 3],[1 3 5:7]) B = 10 8 6 5 4 2 6 10 12 14

26 (Two ways to add elements to existing variables ) Assign values to indices that don't exist: MATLAB expands array to include indices, puts the specified values in the assigned elements, fills any unassigned new elements with zeros. Add values to ends of variables: Adding to ends of variables is called appending or concatenating. "end" of vector is right side of row vector or bottom of column vector. "end" of matrix is right column and bottom row. 26

27 (Assigning to undefined indices of vectors) >> fred=1:4 fred = 1 2 3 4 >> fred(5:10)=10:5:35 fred = 1 2 3 4 10 15 20 25 30 35 >> wilma=[5 7 2] wilma = 5 7 2 >> wilma(8)=4 wilma = 5 7 2 0 0 0 0 4 >> barney(5)=24 barney = 0 0 0 0 24 New elements added Unassigned elements are initialized at zero 27

28 (Appending to vectors) You can only append row vectors to row vectors and column vectors to column vectors. If r1 and r2 are any row vectors, r3 = [r1 r2] is a row vector whose left part is r 1 and right part is r2 If c1 and c2 are any column vectors, c3 = [c1; c2 ] is a column vector whose top part is c1 and bottom part is c2 28

29 >> pebbles = [3 8 1 24]; >> dino = 4:3:16; >> betty = [pebbles dino] betty = 3 8 1 24 4 7 10 13 16 >> bedrock = [pebbles'; dino'] bedrock = 3 8 1 24 4 7 10 13 16 29

30 (Appending to matrices) If appending one matrix to the right side of another matrix, both must have same number of rows. If appending one matrix to the bottom of another matrix, both must have same number of columns 30

31 >> A2=[1 2 3; 4 5 6] A2 = 1 2 3 4 5 6 >> B2=[7 8; 9 10] B2 = 7 8 9 10 >> C2=eye(3) C2 = 1 0 0 0 1 0 0 0 1 31

32 >> Z=[A2 B2] Z = 1 2 3 7 8 4 5 6 9 10 >> Z=[A2; C2] Z = 1 2 3 4 5 6 1 0 0 0 1 0 0 0 1 >> Z=[A2; B2] ??? Error using ==> vertcat CAT arguments dimensions are not consistent. 32

33 To delete elements in a vector or matrix, set range to be deleted to empty brackets. >> kt=[2 8 40 65 3 55 23 15 75 80] kt = 2 8 40 65 3 55 23 15 75 80 >> kt(6)=[] kt = 2 8 40 65 3 23 15 75 80 >> kt(3:6)=[] kt = 2 8 15 75 80 Delete sixth element (55) 55 gone Delete elements 3 through 6 of current kt, not original kt 33

34 To delete elements in a vector or matrix, set range to be deleted to empty brackets. >> mtr=[5 78 4 24 9; 4 0 36 60 12; 56 13 5 89 3] mtr = 5 78 4 24 9 4 0 36 60 12 56 13 5 89 3 >> mtr(:,2:4)=[] mtr = 5 9 4 12 56 3 Delete all rows for columns 2 to 4 34

35 MATLAB has many built-in functions for working with arrays. Some common ones are: length(v) - number of elements in a vector. size(A) - number of rows and columns in a matrix or vector. reshape(A,m,n) - changes number of rows and columns of a matrix or vector while keeping total number of elements the same. For example, changes 4x4 matrix to 2x8 matrix. 35

36 >> a = [1 2 ; 3 4] a = 1 2 3 4 >> a = [a [5 6; 7 8]] a = 1 2 5 6 3 4 7 8 >> reshape (a, 1, 8) ans = 1 3 2 4 5 7 6 8 >> reshape (a, 8, 1) ans = 1 3 2 4 5 7 6 8 reshape Example 36

37 diag(v) - makes a square matrix of zeroes with vector in main diagonal. diag(A) - creates vector equal to main diagonal of matrix. For more functions, in help window select "Functions by Category", then "Mathematics", then "Arrays and Matrices". 37

38 38 >> a=[1 2 3] a = 1 2 3 >> diag(a) ans = 1 0 0 0 2 0 0 0 3 >> B= [1 2 3;2 5 7; 0 0 8] B = 1 2 3 2 5 7 0 0 8 >> diag(B) ans = 1 5 8 Example$


Download ppt "(The Transpose Operator) 1 >> C=[2 55 14 8; 21 5 32 11; 41 64 9 1] C = 2 55 14 8 21 5 32 11 41 64 9 1 >> D=C' D = 2 21 41 55 5 64 14 32 9 8 11 1."

Similar presentations


Ads by Google