Thinking in a Matrix Wav 9 A3x4 matrix A 4x1(column)vector A1X4(row)vector 「353 6 4 [512-21] -93149 7 7 0 -2112 Fig.1.2 Matrices of various dimensions If you are familiar with the spreadsheet software Excel,you can imagine each Excel worksheet as a matrix,with rows and columns. Now let's try to define some matrices and vectors in MATLAB.Type the follow. ing statements as written: >a=[3,5,7,8] <ENTER> a■ 512-2 1 >b=【4;2:7:1] <ENTER> b= 9 7 >c=[3,53,6:12,-93,145:4,7,1:0,-21,121 <ENTER> c= 53 6 12 -93 145 4 0 -21 12 >>whos KENTER> Name Size Bytes Class Attributes 1×4 32 double 6 4×1 32 double 4×3 96 double number 1xl double nd each variable is displayed toge mns.(Note:if you have used other variable previously.your list of variables could be different).Note that a scalar value,like the variable number,can be considered a 1x I matrix
Thinking in a Matrix Way 9 If you are familiar with the spreadsheet software Excel, you can imagine each Excel worksheet as a matrix, with rows and columns. Now let’s try to defi ne some matrices and vectors in MATLAB. Type the following statements as written: >> a=[3,5,7,8] <ENTER> a = 5 12 -2 1 >> b= [4;2;7;1] <ENTER> b = 4 2 7 1 >> c= [3, 53,6;12,-93,145;4,7,1;0,-21,12] <ENTER> c = 3 53 6 12 -93 145 4 7 1 0 -21 12 >> whos <ENTER> Name Size Bytes Class Attributes a 1x4 32 double b 4x1 32 double c 4x3 96 double number 1x1 8 double As you can see, after the command whos , each variable is displayed together with its size expressed in rows × columns. (Note: if you have used other variables previously, your list of variables could be different). Note that a scalar value, like the variable number , can be considered a 1 × 1 matrix. Fig. 1.2 Matrices of various dimensions
10 I Basic Operations Sometimes we need to know the size of the variables only,instead of the full list of their properties.In this case.the function size(c)returns the number of rows and columns of the variable >>size(c) ans 43 Another useful function is length(c),which returns the length of the vector c. If cis a matrix,the function length returns the number of rows only: >>length(c) ans- To put data into a matrix.you must type the values within square brackets.sepa rated by spaces or commas for different elements in a row,while the semicolon (: is used to indicate the end of the row.Note that the number of elements must be the same in each row: >x=[123:2571 <ENTER> x 1 2 5 7 If you have not put the same number of elements in each row,MATLAB displays an error: >>X=「23:2571: ??Error using ==vertcat CAT arguments dimensions are not consistent As you can see,MATLAB is not a wizard who tries putting the missing element place.MATLAB does not know whethe first and second respectively. You can use a matrix or a vector to implement another variable.For example, type in the following statements: >>×=[321]: <ENTER> >y=[6,7,81: <ENTER> <ENTER> KENTER>
10 1 Basic Operations Sometimes we need to know the size of the variables only, instead of the full list of their properties. In this case, the function size(c) returns the number of rows and columns of the variable c : >> size(c) ans = 4 3 Another useful function is length(c), which returns the length of the vector c . If c is a matrix, the function length returns the number of rows only: >> length(c) ans= 4 To put data into a matrix, you must type the values within square brackets, separated by spaces or commas for different elements in a row, while the semicolon (;) is used to indicate the end of the row. Note that the number of elements must be the same in each row: >> x=[ 1 2 3; 2 5 7] <ENTER> x = 1 2 3 2 5 7 If you have not put the same number of elements in each row, MATLAB displays an error: >> x = [2 3; 2 5 7]; ??? Error using ==> vertcat CAT arguments dimensions are not consistent. As you can see, MATLAB is not a wizard who tries putting the missing element in the right place. MATLAB does not know whether you want to put the element 2 and 3 in the fi rst and second columns or in the second and third columns respectively. You can use a matrix or a vector to implement another variable. For example, type in the following statements: >> x = [3 2 1]; <ENTER> >> y = [6,7,8]; <ENTER> >> z1 = [x -y]; <ENTER> >> z2 = [x; -y]; <ENTER>
Thinking in a Matrix Wav Can you work out what 1and 2 will look like before displaying them?In the fol wing ta able we present other examples showing how to use variables already implemented to create new variables Matlab (type after the representation prompt>>followed by Enter) Dimension M=B12π =[3,12,p1]; 1x3 Row vector w- =[3,12,p1:8,9,10] 2×3 Matrix Orequivalently.if you have already inserted M w=:8,9,101: 4 P=4:2-11 3xI Column P=2 vector -1 Q=4.-4:2-2-1,1h 3x2 Matrix Orequivalently.if you have already inserted P: -11 Q-[P:-PJ: If you do not specify any variable content (ie.any values inside the square brackets).MATLAB cr a variable of size zero with no value,or more precisely a matrix of dimension 0x0 with no value in it. >y=【J <ENTER> >whos y <ENTER> Name Size Bytes class Attributes 0x0 0 double The Workspace Browser in the desktop provides a handy visual representation of the workspace.By clicking a variable in the Workspace Browser.we open the Array Editor.which can be used to view and cha values entry that lies in the ith row and the jth olumn of a matrix is typically referred to as the (ij).or (ij)th entry of the matrix.For example,the (3.2)entry of matrix Q in the table above is 1.In mathematical format,it is usually written as Q, while in MATLAB you can access to the matrix entries in this way: >>0(3,2) <ENTER> ans of parentheses.Forindexing you use parentheses.whereas todefine a matrix,you use square brackets;otherwise,you get an error: >0[2,3] ??Q[2,3] Error:Unbalanced or unexpected parenthesis or bracket
Thinking in a Matrix Way 11 Can you work out what z1 and z2 will look like before displaying them? In the following table we present other examples showing how to use variables already implemented to create new variables: Mathematical representation MATLAB (type after the prompt >> followed by Enter) Dimension M=[3,12,pi]; 1 × 3 Row vector 3 12 8 9 10 N ⎡ ⎤ p = ⎢ ⎥ ⎣ ⎦ N=[3,12,pi; 8,9,10]; 2 × 3 Matrix Or equivalently, if you have already inserted M: N=[M; 8,9,10]; 4 2 1 P ⎡ ⎤ ⎢ ⎥ = ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ − P=[4;2;−1] 3 × 1 Column vector 4 4 2 2 1 1 Q ⎡ ⎤ − ⎢ ⎥ = − ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ − Q=[4, −4;2, −2;−1, 1]; 3 × 2 Matrix Or equivalently, if you have already inserted P: Q=[P;−P]; If you do not specify any variable content (i.e., any values inside the square brackets), MATLAB creates a variable of size zero with no value, or more precisely, a matrix of dimension 0 × 0 with no value in it. >> y = [ ]; <ENTER> >> whos y <ENTER> Name Size Bytes Class Attributes y 0x0 0 double The Workspace Browser in the desktop provides a handy visual representation of the workspace. By clicking a variable in the Workspace Browser, we open the Array Editor, which can be used to view and change values. The entry that lies in the i th row and the j th column of a matrix is typically referred to as the ( i , j ), or ( i , j )th entry of the matrix. For example, the (3,2) entry of matrix Q in the table above is 1. In mathematical format, it is usually written as Q 3,2 , while in MATLAB you can access to the matrix entries in this way: >> Q(3,2) <ENTER> ans = 1 Note the use of parentheses. For indexing you use parentheses, whereas to defi ne a matrix, you use square brackets; otherwise, you get an error: >>Q[2,3] ??? Q[2,3] | Error: Unbalanced or unexpected parenthesis or bracket. M = [ ] 3 12 p
12 I Basic Operations In the following table you can find other examples: MATLAB(type after the Mathematical representation prompt >followed by Enter) is equal to-1 >0(3,1) ans= N is equal to9 M,is equal to元 3.1416 >(2,1) B.is equal to 2 ans= 2 es bi only the single instead of N(2,1). If you refer to an element in a nonexistent position,MATLAB gives you an alert: >>043.3) <ENTER> ??Index exceeds matrix dimensions What har ens if you want to address more than one element at time?This is pos sing a vector (or a matrix)in the indexing place to express the selected rows or columns >Q([1,3],2) <ENTER> ans■ -4 1 How many values do you expect MATLAB to display when you type Q([1.3].[1.2))? Two or Four?Let's try >Q([1,31,[1,21 <ENTER> ans 9 -1 1 The answer is four,because MATLAB shows the values in the positions given by Now supp a larg matrix from which you ract elements row to t e jth row in the seco m.MATLAB offers a very to this.namely the colon (operator Before see us generate a new matrix: >×=[123:456:789:101112;131415] <ENTER>
12 1 Basic Operations In the following table you can fi nd other examples: Mathematical representation MATLAB (type after the prompt >> followed by Enter) Q3,1 is equal to −1 >>Q(3,1) ans= -1 N2,2 is equal to 9 >> N(2,2) ans= 9 M1,3 is equal to p >> M(1,3) ans= 3.1416 P2,1 is equal to 2 >> N(2,1) ans= 2 If P and M are two vectors, it is possible to refer to their entries by referencing only their single dimension, i.e., you can type M(3) instead of M(1,3), and N(2) instead of N(2,1). If you refer to an element in a nonexistent position, MATLAB gives you an alert: >> Q(3,3) <ENTER> ??? Index exceeds matrix dimensions. What happens if you want to address more than one element at time? This is possible in MATLAB using a vector (or a matrix) in the indexing place to express the selected rows or columns: >> Q([1,3],2) <ENTER> ans = -4 1 How many values do you expect MATLAB to display when you type Q([1,3],[1,2]) ? Two or Four? Let’s try: >> Q([1,3],[1,2]) <ENTER> ans = 4 -4 -1 1 The answer is four, because MATLAB shows the values in the positions given by each combination of the specifi ed rows and columns, i.e., Q 11 , Q 12 , Q 31 , Q 32 . Now suppose you have a large matrix from which you want to extract elements going from the ith row to the jth row in the second column. MATLAB offers a very effi cient way to this, namely the colon ( : ) operator. Before seeing how it works, let us generate a new matrix: >> x=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15] <ENTER>
Thinking in a Matrix Way 13 1 3 91 1215 Now type: >1=2;j=4: <ENTER> >×(1:j,2) <ENTER> ans Note that more than one command has been typed on the first line.This can be done by separating commands with a semicolon.In addition,note that MATLAB displays exactly the values from the second row to the fourth row in the second columns.This is equivalent to: >x([234],2) <ENTER> ans 5 8 11 As a matter of fact,using the colon operator is equivalent to generating a vector going from a given value to another one,possible using a prescribed increment (step).The rule is: Start:Step:Stop Type the following commands: To TYPE after prompt >>followed by Enter MATLAB answer Meaning of the operation 2525 ans Generate a vector going from 2 to 25 27121722 which isgr er tha exceeds the Stop value (i.e.5 i:j 234 MATLAB uses the default value l 10:-3:-5 ans Generate a vector going from 10 to-5. 10741-2-5 increasing the first value by-5.This is quivalent to generating a vector of decreasing values
Thinking in a Matrix Way 13 x = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Now type: >> i=2; j=4; <ENTER> >> x(i:j,2) <ENTER> ans = 5 8 11 Note that more than one command has been typed on the fi rst line. This can be done by separating commands with a semicolon. In addition, note that MATLAB displays exactly the values from the second row to the fourth row in the second columns. This is equivalent to: >> x([2 3 4],2) <ENTER> ans = 5 8 11 As a matter of fact, using the colon operator is equivalent to generating a vector going from a given value to another one, possible using a prescribed increment (step). The rule is: Start:Step:Stop Type the following commands: To TYPE after prompt >> followed by Enter MATLAB answer Meaning of the operation 2:5:25 ans = 2 7 12 17 22 Generate a vector going from 2 to 25 incremented by 5. Note that 22 + 5 = 27, which is greater than 25. MATLAB will generate numbers until it reaches or exceeds the Stop value (i.e., 25) i:j ans = 2 3 4 Generate a vector going from 2 to 4. Here the step value is not specifi ed, and MATLAB uses the default value 1 10:−3:−5 ans = 10 7 4 1 -2 -5 Generate a vector going from 10 to −5, increasing the fi rst value by −5. This is equivalent to generating a vector of decreasing values