资源预览内容
第1页 / 共32页
第2页 / 共32页
第3页 / 共32页
第4页 / 共32页
第5页 / 共32页
第6页 / 共32页
第7页 / 共32页
第8页 / 共32页
第9页 / 共32页
第10页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Numeric and Structure Arrays,Specification of a position vector using Cartesian coordinates. Figure 2.11,The vector p can be specified by three components: x, y, and z, and can be written as: p = x, y, z. However, MATLAB can use vectors having more than three elements.,To create a row vector, separate the elements by semicolons. For example, p = 3,7,9 p = 3 7 9 You can create a column vector by using the transpose notation (). p = 3,7,9 p = 3 7 9,You can also create a column vector by separating the elements by semicolons. For example, g = 3;7;9 g = 3 7 9,2-4,The colon operator (:) easily generates a large vector of regularly spaced elements. Typing x = m:q:n creates a vector x of values with a spacing q. The first value is m. The last value is n if m - n is an integer multiple of q. If not, the last value is less than n.,For example, typing x = 0:2:8 creates the vector x = 0,2,4,6,8, whereas typing x = 0:2:7 creates the vector x = 0,2,4,6. To create a row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = 5:0.1:8. If the increment q is omitted, it is presumed to be 1. Thus typing y = -3:2 produces the vector y = -3,-2,-1,0,1,2.,2-7,The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment. The syntax is linspace(x1,x2,n), where x1 and x2 are the lower and upper limits and n is the number of points. For example, linspace(5,8,31) is equivalent to 5:0.1:8. If n is omitted, the spacing is 1.,For example, if x = 2,-4,5, its length is 3; (computed from length(x) its magnitude is 22 + (4)2 + 52 = 6.7082; (computed from sqrt(x*x) its absolute value is 2,4,5 (computed from abs(x).,Matrices A matrix has multiple rows and columns. For example, the matrix has four rows and three columns. Vectors are special cases of matrices having one row or one column.,M =,2 4 10 16 3 7 8 4 9 3 12 15,Creating Matrices If the matrix is small you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing A = 2,4,10;16,3,7; creates the following matrix: 2 4 10 16 3 7 Remember, spaces or commas separate elements in different columns, whereas semicolons separate elements in different rows.,A =,Array Addition and Subtraction,6 2 10 3,+,9 8 12 14,=,15 6 2 17,Array subtraction is performed in a similar way. The addition shown in equation 2.31 is performed in MATLAB as follows: A = 6,-2;10,3; B = 9,8;-12,14 A+B ans = 15 6 -2 17,For example:,(2.3-1),Geometric interpretation of scalar multiplication of a vector.,If r = x, y, z, then v = 2r =2x, y, z = 2x, 2y, 2z.,Multiplying a matrix A by a scalar w produces a matrix whose elements are the elements of A multiplied by w. For example:,3,2 9 5 7,=,6 27 15 21,This multiplication is performed in MATLAB as follows: A = 2, 9; 5,-7; 3*A ans = 6 27 15 -21,MATLAB uses two definitions of multiplication: array multiplication (also called element-by-element multiplication), and matrix multiplication.,Division and exponentiation must also be carefully defined when you are dealing with operations between two arrays. MATLAB has two forms of arithmetic operations on arrays. Next we introduce one form, called array operations, which are also called element-by-element operations. Then we will introduce matrix operations. Each form has its own applications.,Element-by-element operations,Symbol + - + - .* ./ . .,Examples 6,3+2=8,5 8,3-5=3,-2 6,5+4,8=10,13 6,5-4,8=2,-3 3,5.*4,8=12,40 2,5./4,8=2/4,5/8 2,5.4,8=24,58 3,5.2=32,52 2.3,5=23,25 3,5.2,4=32,54,Operation Scalar-array addition Scalar-array subtraction Array addition Array subtraction Array multiplication Array right division Array left division Array exponentiation,Form A + b A b A + B A B A.*B A./B A.B A.B,Array or Element-by-element multiplication is defined only for arrays having the same size. The definition of the product x.*y, where x and y each have n elements, is x.*y = x(1)y(1), x(2)y(2), . , x(n)y(n) if x and y are row vectors. For example, if x = 2, 4, 5, y = 7, 3, 8 (2.34) then z = x.*y gives z = 2( 7), 4 (3), 5(8) = 14, 12, 40,2-33,If x and y are column vectors, the result of x.*y is a column vector. For example z = (x).*(y) gives,Note that x is a column vector with size 3 1 and thus does not have the same size as y, whose size is 1 3.,2(7) 4(3) 5(8),14 12 40,=,z =,The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements ci j = ai j bi j . For example, if,then C = A.*B gives this result:,A =,11 5 9 4,B =,7 8 6 2,C =,11(7) 5(8) 9(6) 4(2),=,77 40 54 8,The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result the same size as the array argument x. Thus these functions are said to be vectorized functions. For example, in the following session the result y has the same size as the argument x. x = 4, 16, 25; y = sqrt(x) y = 2 4 5,
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号