Charpter 1 Basic Matlab following command lists the first 4 entries of the vector v,using parentheses: the increment value 2: brackets:中括号 》v(1:2:4) omit:省略 ans= convention:惯例 04 algebra:代数 Defining a matrix is similar to defining a vector.To define a notation: matrix A,you can treat it like a column of row vectors.That is,carriage: you enter each row of the matrix as a row vector (remember to separate the entries either by commas or spaces)and you separate the rows by semicolons (;) 》A=[123,345;678] A= 123 345 678 We can avoid separating each row with a semicolon if we use a carriage return instead.In other words,we could have defined A as follows 》A=[ 123 345 678] A= 123 345 678 which is perhaps closer to the way we would have defined A by hand using the linear algebra notation. You can refer to a particular entry in a matrix by using parentheses.For example,the number 5 lies in the 2nd row,3rd column of A,thus 》A(2,3) ans= 5 The order of rows and columns follows the convention adopted in the linear algebra notation.This means that A(2,3) refers to the number 5 in the above example and A(3,2)refers to the number 7,which is in the 3rd row,2nd column. Note MATLAB's response when we ask for the entry in the 4th row,1st column 》A(4,1) ??Index exceeds matrix dimensions. As expected,we get an error message.Since A is a 3-by-3 matrix,there is no 4th row and MATLAB realizes that.The error
Charpter 1 Basic Matlab following command lists the first 4 entries of the vector v, using the increment value 2 : » v(1:2:4) ans = 0 4 Defining a matrix is similar to defining a vector. To define a matrix A, you can treat it like a column of row vectors. That is, you enter each row of the matrix as a row vector (remember to separate the entries either by commas or spaces) and you separate the rows by semicolons (;). » A = [1 2 3; 3 4 5; 6 7 8] A = 1 2 3 3 4 5 6 7 8 We can avoid separating each row with a semicolon if we use a carriage return instead. In other words, we could have defined A as follows » A = [ 1 2 3 3 4 5 6 7 8] A = 1 2 3 3 4 5 6 7 8 which is perhaps closer to the way we would have defined A by hand using the linear algebra notation. You can refer to a particular entry in a matrix by using parentheses. For example, the number 5 lies in the 2nd row, 3rd column of A, thus » A(2,3) ans = 5 The order of rows and columns follows the convention adopted in the linear algebra notation. This means that A(2,3) refers to the number 5 in the above example and A(3,2) refers to the number 7, which is in the 3rd row, 2nd column. Note MATLAB’s response when we ask for the entry in the 4th row, 1st column. » A(4,1) ??? Index exceeds matrix dimensions. As expected, we get an error message. Since A is a 3-by-3 matrix, there is no 4th row and MATLAB realizes that. The error parentheses:圆括号 brackets:中括号 omit:省略 convention:惯例 algebra:代数 notation:符号 carriage:回车
Charpter 1 Basic Matlab messages that we get from MATLAB can be quite informative index:下标,索引 when trying to find out what went wrong.In this case MATLAB ,exceed:超出 told us exactly what the problem was. dimension:维数 We can"extract"submatrix using a similar notation as above. submatrix:子矩阵 For example to obtain the submatrix that consists of the first two quite informative: rows and last two columns of A we type 当丰富 》A(1:2,2:3) extract:提取 ans= ease:方便 23 45 We could even extract an entire row or column of a matrix, using the colon (:as follows. Suppose we want to get the 2nd column of A.We basically want the elements [A(1,2)A(2,2)A(3,2)].We type 》AC,2) ans= 2 4 7 where the colon was used to tell MATLAB that all the rows are to be used.The same can be done when we want to extract an entire row,say the 3rd one. 》A(3,) ans= 678 Define now another matrix B,and two vectors s and t that will be used in what follows. 》B=[ -1310 -9525 0142] B= -1310 -9525 0142 》s=[-185] s= -185 》t=[7:0,11] t= 7 0 11 The real power of MATLAB is the ease in which you can
Charpter 1 Basic Matlab messages that we get from MATLAB can be quite informative when trying to find out what went wrong. In this case MATLAB told us exactly what the problem was. We can “extract” submatrix using a similar notation as above. For example to obtain the submatrix that consists of the first two rows and last two columns of A we type » A(1:2,2:3) ans = 2 3 4 5 We could even extract an entire row or column of a matrix, using the colon (:) as follows. Suppose we want to get the 2nd column of A. We basically want the elements [A(1,2) A(2,2) A(3,2)]. We type » A(:,2) ans = 2 4 7 where the colon was used to tell MATLAB that all the rows are to be used. The same can be done when we want to extract an entire row, say the 3rd one. » A(3,:) ans = 6 7 8 Define now another matrix B, and two vectors s and t that will be used in what follows. » B = [ -1 3 10 -9 5 25 0 14 2] B = -1 3 10 -9 5 25 0 14 2 » s = [-1 8 5] s = -1 8 5 » t = [7;0;11] t = 7 0 11 The real power of MATLAB is the ease in which you can index:下标,索引 exceed:超出 dimension:维数 submatrix:子矩阵 quite informative:相 当丰富 extract:提取 ease:方便
Charpter 1 Basic Matlab manipulate your vectors and matrices. For example,to subtract 1 from every entry in the matrix A manipulate:( type 操纵 》A-1 subtract:减 ans= compatible:相匹配 012 multiplication:乘法 234 invertible:可逆的 567 It is just as easy to add (or subtract)two compatible matric (i.e.matrices of the same size). 》A+B ans= 0513 -6930 62110 The same is true for vectors. 》s-t ??Error using ==> Matrix dimensions must agree. This error was expected,since s has size 1-by-3 and t has size 3-by-1.We will not get an error if we type 》s-t ans= -88-6 since by taking the transpose of t we make the two vectors compatible. We must be equally careful when using multiplication. 》B*s ??Error using => Inner matrix dimensions must agree. 》B*t ans= 103 212 22 Another important operation that MATLAB can perform with ease is "matrix division".If M is an invertible square matrix and b is a compatible vector then x=Mb is the solution of Mx=b and x=b/M is the solution of x M=b. Let us illustrate the first of the two operations above with M= B and b=t. 》X=B\t X=
Charpter 1 Basic Matlab manipulate your vectors and matrices. For example, to subtract 1 from every entry in the matrix A we type » A-1 ans = 0 1 2 2 3 4 5 6 7 It is just as easy to add (or subtract) two compatible matrices (i.e. matrices of the same size). » A+B ans = 0 5 13 -6 9 30 6 21 10 The same is true for vectors. » s-t ??? Error using ==> - Matrix dimensions must agree. This error was expected, since s has size 1-by-3 and t has size 3-by-1. We will not get an error if we type » s-t' ans = -8 8 -6 since by taking the transpose of t we make the two vectors compatible. We must be equally careful when using multiplication. » B*s ??? Error using ==> * Inner matrix dimensions must agree. » B*t ans = 103 212 22 Another important operation that MATLAB can perform with ease is “matrix division”. If M is an invertible square matrix and b is a compatible vector then x = M\b is the solution of M x = b and x = b/M is the solution of x M = b. Let us illustrate the first of the two operations above with M = B and b = t. » x=B\t x = manipulate:(方便地) 操纵 subtract:减 compatible:相匹配 multiplication:乘法 invertible:可逆的
Charpter 1 Basic Matlab 2.4307 illustrate:举例说明 0.6801 integer::整数 0.7390 incompatibility:(维数) x is the solution of B x =t as can be seen in the multiplication 不匹配 below. element:元素 》B*X decimal point:小数点 ans= a real number::实数 7.0000 element-wise” 0.0000 operation:对矩阵元素 11.0000 的操作 Since x does not consist of integers,it is worth while period:句号 mentioning here the command "format long".MATLAB only displays four digits beyond the decimal point of a real number unless we use the command format long,which tells MATLAB to display more digits. 》format long 》X X= 2.43071593533487 0.68013856812933 0.73903002309469 On a PC the command format long can also be used through the Window Menus. There are many times when we want to perform an operation to every entry in a vector or matrix. MATLAB will allow us to do this with "element-wise" operations.For example,suppose you want to multiply each entry in the vectors with itself.In other words,suppose you want to obtain the vector s2 [s(1)*s(1),s(2)*s(2),s(3)*s(3)].The command s*s will not work due to incompatibility.What is needed here is to tell MATLAB to perform the multiplication element-wise.This is done with the symbols ".*"In fact,you can put a period in front of most operators to tell MATLAB that you want the operation to take place on each entry of the vector (or matrix). 》s*s ??Error using => Inner matrix dimensions must agree. 》S.*s ans= 16425 The symbol ".can also be used since we are after all raisings to a power.(The period is needed here as well.) 》s.2
Charpter 1 Basic Matlab 2.4307 0.6801 0.7390 x is the solution of B x = t as can be seen in the multiplication below. » B*x ans = 7.0000 0.0000 11.0000 Since x does not consist of integers, it is worth while mentioning here the command “format long”. MATLAB only displays four digits beyond the decimal point of a real number unless we use the command format long, which tells MATLAB to display more digits. » format long » x x = 2.43071593533487 0.68013856812933 0.73903002309469 On a PC the command format long can also be used through the Window Menus. There are many times when we want to perform an operation to every entry in a vector or matrix. MATLAB will allow us to do this with “element-wise” operations. For example, suppose you want to multiply each entry in the vectors with itself. In other words, suppose you want to obtain the vector s2 = [s(1)*s(1), s(2)*s(2), s(3)*s(3)]. The command s*s will not work due to incompatibility. What is needed here is to tell MATLAB to perform the multiplication element-wise. This is done with the symbols ".*". In fact, you can put a period in front of most operators to tell MATLAB that you want the operation to take place on each entry of the vector (or matrix). » s*s ??? Error using ==> * Inner matrix dimensions must agree. » s.*s ans = 1 64 25 The symbol " .^ " can also be used since we are after all raisings to a power. (The period is needed here as well.) » s.^2 illustrate:举例说明 integer:整数 incompatibility(维数) : 不匹配 element:元素 decimal point:小数点 a real number:实数 “element-wise” operation:对矩阵元素 的操作 period:句号