14 I Basic Operation You now have three equivalent ways of accessing the third-row entries of x: >×(3,[1231) <ENTER> ans 9 >x(3,1:3) <ENTER> ans 789 >x(3,:) <ENTER> ans 789 In the last case ecify the start and stop values when you use if you need the first column only,you can type either x([1,2,3,4,51,1)or x([1:5],1)0r×(:,1); By using the colon operator together with the empty array,we are able to delete entire rows or entire columns.For example,to delete the entire second column of x and then its third and fourth rows,type: <ENTER> 1 47 10 12 1315 >×([3,4],:)=【1 <ENTER> X 3 g 15 Note that you cannot delete a single entry in a matrix because that would lead to an ambiguity in its dimensions.So a statement like x(1,2)=[I returns an error: >×(1,2)=[1 ??Subscripted assignment dimension mismatch. We conclude this paragraph by mentioning a way to create a matrix using index ing.In contrast to。 we do eed to cll to MATLAB is,ete.)before using it.MATLAB t type able,how large i B creates the variable on the fly.So if you want to insert the response time to the stimulus number 10.you can simply type: >AnsTime(10)=1.34 AnsTime 0 0 0 000. 0 0 01.3400
14 1 Basic Operations You now have three equivalent ways of accessing the third-row entries of x: >> x(3,[1 2 3]) <ENTER> ans = 7 8 9 >> x(3,1:3) <ENTER> ans = 7 8 9 >> x(3,:) <ENTER> ans = 7 8 9 In the last case, you do not need to specify the start and stop values when you use the colon operator. MATLAB assumes that you mean the entire row. Analogously, if you need the fi rst column only, you can type either x([1,2,3,4,5],1) or x([1:5],1) or x(:,1); By using the colon operator together with the empty array, we are able to delete entire rows or entire columns. For example, to delete the entire second column of x and then its third and fourth rows, type: >> x(:,2) = [ ] <ENTER> x = 1 3 4 6 7 9 10 12 13 15 >> x([3,4],:) =[ ] <ENTER> x = 1 3 4 6 13 15 Note that you cannot delete a single entry in a matrix because that would lead to an ambiguity in its dimensions. So a statement like x(1,2)=[ ] returns an error: >> x(1,2)=[ ] ??? Subscripted assignment dimension mismatch. We conclude this paragraph by mentioning a way to create a matrix using indexing. In contrast to other computer languages, in MATLAB we do not need to declare a variable (i.e., tell to MATLAB what type of variable, how large it is, etc.) before using it. MATLAB creates the variable on the fl y. So if you want to insert the response time to the stimulus number 10, you can simply type : >> AnsTime(10)=1.34 AnsTime = 0 0 0 0 0 0 0 0 0 1.3400
Operations 15 MATLAB automatically creates the variable called AnsTime and put the numbe you ha default Operations There are operations that can be applied to modify the contents of a matrix without changing the number of elements.These operations are matrix addition,scalar mul- on,and transposition.These form the basic techniques for dealing with trices,as displayed in the following table: Operation Definition Math example Matlabexample Addition The result of A+B >>A=15:2.31 (subtraction) or (A-B)is calculated >>B=「23:4.11 >>A+B A+8=[习 ans 8 the element in A 4 4-8日到 >>A-B ans -22 multiplication by a matrix Cis c-8-s >>g*C ained by c-e周 ans Transposition D=B22 nxm matrix denoted 8910 by Dobtained by [3 8 ng rows int D=12 n 10 into rows 210 Multiplication of a matrix by another matrix is more complicated.Multiplication of two matrices is well defined only if the number of columns of the left-hand matrix is the same as the number of rows of the right-hand matrix.Matrix multipli- cation may seem complex,and perhaps you will not use it very often.Howeve er it out to he useful when one of the rices is a vector,so we give you the fol- lowin definition: matrix and B is an nxpr atrix,then their matrix product AB is the mxp matrix whose entries are given by the following equation. (AB-∑AB
Operations 15 MATLAB automatically creates the variable called AnsTime and put the number you have entered in position 10. The unspecifi ed values are fi lled with zeros by default. Operations There are operations that can be applied to modify the contents of a matrix without changing the number of elements. These operations are matrix addition , scalar multiplication , and transposition . These form the basic techniques for dealing with matrices, as displayed in the following table: Operation Defi nition Math example Matlab example Addition (subtraction) The result of A + B or ( A−B ) is calculated entrywise, i.e., the element B i,j is added to (subtracted from) the element in A i,j 15 23 , 23 41 3 8 6 4 1 2 2 2 A B A B A B ⎡⎤ ⎡⎤ = = ⎢⎥ ⎢⎥ ⎣⎦ ⎣⎦ ⎡ ⎤ + = ⎢ ⎥ ⎣ ⎦ ⎡ ⎤ − − = ⎢ ⎥ ⎣ ⎦ − >>A=[1,5;2,3]; >>B=[2,3;4,1]; >>A+B ans = 3 8 6 4 >> A-B ans = -1 2 -2 2 Scalar multiplication The multiplication of a scalar (= number) s by a matrix C is obtained by multiplying every entry of C by s 3 2 , 4 4 1 12 8 16 4 C s s C ⎡ ⎤ = = ⎢ ⎥ ⎣ ⎦ ⎡ ⎤ ⋅ = ⎢ ⎥ ⎣ ⎦ >>C=[3,2;4,1]; >>s=4; >>s*C ans = 12 8 16 4 Transposition The transpose of an m × n matrix D is an n × m matrix denoted by DT obtained by turning rows into columns and columns into rows 3 12 2 8 9 10 3 8 12 9 2 10 T D D ⎡ ⎤ = ⎢ ⎥ ⎣ ⎦ ⎡ ⎤ ⎢ ⎥ = ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ >> D=[2,12,2;8,9,10]; >> D’ ans = 3 8 12 9 2 10 Multiplication of a matrix by another matrix is more complicated. Multiplication of two matrices is well defi ned only if the number of columns of the left-hand matrix is the same as the number of rows of the right-hand matrix. Matrix multiplication may seem complex, and perhaps you will not use it very often. However, it turns out to be useful when one of the matrices is a vector, so we give you the following defi nition: If A is an m × n matrix and B is an n × p matrix, then their matrix product AB is the m × p matrix whose entries are given by the following equation. = , , = ∑1 ( ) n i j ir r j r AB A B
16 I Basic Operations Fig.1.3 Visual interpretation of matrix product The ith row of A is multiplied succes sively by e pretation of the previous equation. We reiterate that a matrix product can be performed only if the number of col- umns in the first matrix(A)is equal to the number of rows of the second matrix(B). For example,using the matrices entered in the previous table,it is not possible to perform the product DC (try to verify this with MATLAB): ??Error using -mtimes Inner matrix dimensions must agree. However,you can compute the product CD.The result is a matrix having the same number of rows as the first matrix(C)and the same number of columns as the second matrix (D).We see,then,that in general,for two matrices CDDC,and indeed,one of these products might not even be defined.But even if C and D are square matrices,it is generally the case that CD+DC: >C+D <ENTER> ans 22 26 18
16 1 Basic Operations The equation means that each element of the ith row of A is multiplied successively by each element of the jth column of B . In Fig. 1.3 you can see a visual interpretation of the previous equation. We reiterate that a matrix product can be performed only if the number of columns in the fi rst matrix ( A ) is equal to the number of rows of the second matrix ( B ). For example, using the matrices entered in the previous table, it is not possible to perform the product DC (try to verify this with MATLAB): >> D*C ??? Error using ==> mtimes Inner matrix dimensions must agree. However, you can compute the product CD. The result is a matrix having the same number of rows as the fi rst matrix ( C ) and the same number of columns as the second matrix ( D ). We see, then, that in general, for two matrices CD ¹ DC , and indeed, one of these products might not even be defi ned. But even if C and D are square matrices, it is generally the case that CD ¹ DC : >> C*D <ENTER> ans = 22 54 26 16 57 18 Fig. 1.3 Visual interpretation of matrix product
Summary e elements of A in position ij by the elements of B in the same i.i position using the element-by-element operators. as shown in the following table: Description MATLAB operator Example Element-by-element Multiplication >A.*B ans- 15 83 Element-by-element Right division 0.5000 1.6667 0.5000 3.0000 Element-by-element Left division >>A,1B ans Element-by-element Exponentiation 125 16 3 For those who are familiar with matrix e other poible opration Here are some of the eTen ucuo im(A),the determinant function der(A),the eigenvalue function eig(A),the singular value decomposition function svd(A),the LU factorization lu(A). Summary .MATLAB an be thought of as a scientific calculator:you can perform opera ions from s mple to complex simply by typing them into the command line of the command window;operations are calculated immediately. The six arithmetic operators for scalars are+-*\/and They operate according to rules of precedence.Parentheses have the highest precedence. To store numbers or operation results you need variables. Variable names consists only of letters,digits,and underscores,and must start with a letter maTlab inte rets uppercase and lowercase as different letters age The com lists the variable in the workspace.To delete variables use foulowed by the name of variables.oramivelycr every variable
Summary 17 An element-by-element operation similar to matrix summation is also available in MATLAB. Let’s say that we want to multiply the elements of A in position i,j by the elements of B in the same i,j position using the element-by-element operators , as shown in the following table: Description MATLAB operator Example Element-by-element Multiplication .* >> A.*B ans= 2 15 8 3 Element-by-element Right division ./ >> A./B ans = 0.5000 1.6667 0.5000 3.0000 Element-by-element Left division .\ >> A.\B ans = 2.0000 0.6000 2.0000 0.3333 Element-by-element Exponentiation .^ >> A.^B ans = 1 125 16 3 For those who are familiar with matrix equations, MATLAB has a huge number of other possible operations. Here are some of the basic functions: The inverse function inv(A) , the determinant function det(A) , the eigenvalue function eig(A) , the singular value decomposition function svd(A) , the LU factorization lu(A) . Summary • MATLAB can be thought of as a scientifi c calculator: you can perform operations from simple to complex, simply by typing them into the command line of the command window; operations are calculated immediately. • The six arithmetic operators for scalars are + − * \ / and ˆ . They operate according to rules of precedence. Parentheses have the highest precedence. • To store numbers or operation results you need variables. • Variable names consists only of letters, digits, and underscores, and must start with a letter. MATLAB interprets uppercase and lowercase as different letters (e.g., AgE is different from age) . • The command whos lists the variable in the workspace. To delete variables use clear followed by the name of variables, or alternatively, clear all to clear every variable
18 I Basic Operations .MATLAB refers to all variables as matrices: An NxM matrix is an array having nrows and M column A vector can be a 1xN matrix (row vector)or an Nx1 matrix (column vector) 。A scalar is a 1×1matrix .Vectors and matrices are entered in square brackets.Elements are separated by spaces or commas.Rows are separated by semicolons. An element of a matrix is referred to by a pair of numbers in parentheses indicat- ing its position.An element of a vector can be referenced using simply a number. A range of elements can be referred to using vectors instead numbers in arentheses TheColo vector going om value to another,possibly using a specified increment (step):start:step:stop Pay attention when using Matrix operations especially to the dimensions of the matrices involved. The basic matrix operations are addition(+),scalar multiplication(*),and trans- position ( Element-by-element operations between matrices of the same dimension can be earried out using the operators. and .1 Exercises 1.Evaluate the following expressions: Solution(to type after Mathematical expression prompt >followed by Enter) Result 2.4/12*2 2★4/12+2A2 ans=2.6667 23+5/2-7. 2*3+5/2-7.5 ans-1 6 sqrt(16) ans=4 13·3 (13*3)/(12-4) ans=4.875 12-4 ex2sin (2*pi+4)-2*sin(pi/4)/(12-3) ans=0.9854 12-3 22+12 Try by yourself.The result ang=4.2500 12/3+4 must be the same V2+32-2 Try by yourself.The result ans=4.7958 must be the same
18 1 Basic Operations • MATLAB refers to all variables as matrices: ◦ An N × M matrix is an array having N rows and M columns. ◦ A vector can be a 1 × N matrix (row vector) or an N × 1 matrix (column vector) ◦ A scalar is a 1 × 1 matrix • Vectors and matrices are entered in square brackets . Elements are separated by spaces or commas. Rows are separated by semicolons. • An element of a matrix is referred to by a pair of numbers in parentheses indicating its position. An element of a vector can be referenced using simply a number. A range of elements can be referred to using vectors instead numbers in parentheses. • The Colon operator is equivalent to generating a vector going from one value to another, possibly using a specifi ed increment (step): start:step:stop • Pay attention when using Matrix operations especially to the dimensions of the matrices involved. • The basic matrix operations are addition (+), scalar multiplication (*), and transposition (‘). • Element-by-element operations between matrices of the same dimension can be carried out using the operators ./ .\ .* and .^ . Exercises 1. Evaluate the following expressions: Mathematical expression Solution (to type after prompt >> followed by Enter) Result . 2 2 4/12*2 2*4/12*2^2 ans = 2.6667 2.3+5/2-7. 2*3+5/2−7.5 ans = 1 16 sqrt(16) ans = 4 13 3. 12-4 (13*3)/(12−4) ans = 4.875 ( ) ⎛ ⎞ ⋅ ⎜ ⎟ ⎝ ⎠ 2 +4 -2 sin 2 12-3 p p ((2*pi+4)−2*sin(pi/4))/(12−3) ans = 0.9854 22+12 12/3+4 Try by yourself. The result must be the same ans = 4.2500 ( )2 2+3 -2 Try by yourself. The result must be the same ans = 4.7958