【正文】
rsity 17 Initializing with Builtin Functions zeros and size a = zeros (2)。 c = [1 2。 d = zeros ( size (c) )。 ?Enter a single number。 ?Only Enter key pressed。 Enter data : Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 20 Quiz Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 21 Multidimensional Array Suit for function of one independent variable. R o w 1R o w 2R o w 3R o w 4Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 22 R ow 1R ow 2R ow 3R ow 4C ol 1 C ol 2 C ol 3 C ol 4 C ol 5Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 23 Multidimensional Arrays: ?One subscript for each dimension ?Individual element selected by each subscript ?Total number of elements is the product of each maximum subscript. c (:, :, 1) = [1 2 3。 c (:, :, 2) = [7 8 9。 whos c c Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 24 Storing Multidimensional Arrays in Memory Column major order 1 34 67 910 121471025811 25811Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 25 Multidimensional Arrays: 2 2 2 array (1, 1, 1), (2, 1, 1), (1, 2, 1), (2, 2, 1), (1, 1, 2), (2, 1, 2), (1, 2, 2), (2, 2, 2) First array subscripts incremented most rapidly。 4 5 6。 10 11 12] a = 1 2 3 4 5 6 7 8 9 10 11 12 a(5) = ? Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 27 Good Programming Practice Always use the proper number of dimensions when addressing a multidimensional array. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 28 Subarrays arr1 = [ ] arr1(3) = arr1( [1 4] ) = [ ] arr1( 1 : 2 : 5) = [ ] arr2 = [1 2 3。 3 4 5] arr2(1, :) = [1 2 3] arr2(:, 1 : 2 : 3) 1 2 32 3 43 4 5????? ? ?????132435????????????Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 29 The end Function end returns the highest value taken on by that subscripts. arr1 = [1 2 3 4 5 6 7 8]。 5 6 7 8。 arr4(2 : end, 2 : end) = Each end value is ? 6 7 810 11 12??????Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 30 Using Subarrays on LeftHand Side of an Assignment Statement arr4 = [1 2 3 4。 9 10 11 12] arr4 = 1 2 3 4 5 6 7 8 9 10 11 12 arr4(1 : 2, [1 4])= [20 21。 ??? In an assignment A(matrix, matrix) = B, the number of rows in B and the number of elements in the A row index matrix must be the same. Programming Pitfalls For assignment statement involving subarrays, the shape of the subarrays on both sides of the equal sign must match! Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 32 Assigning a Scalar to a Subarray Scalar value is copied into every element. arr4 = [1 2 3 4。 9 10 11 12]。 disp (str)。The value of pi is % \n39。 str = [39。 num2str(x)]。 fprintf(39。,x)。 0]。 Store the value of expression into location variable_name. ?=‘ : assignment operator. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 53 Operation Algebraic Form MATLAB Form Addition a + b a + b Subtraction a – b a – b Multiplication a b a * b Division a / b Exponentiation a ^ b abba Scalar Operations Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 54 Parentheses can be used to group terms whenever desired. 2 ^ ( ( 8 + 2 ) / 5 ) BAChapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 55 Array and Matrix Operations ? Array Operations Elementbyelement basis! 1. Array and array The number of rows and columns in both arrays must be the same. ??????? 42 31a1324a??? ????1321b?????????0605ab???? ????Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 56 2. Array and scalar ??????? 43 21a1234a??? ????56478a???? ????Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 57 ?Matrix Operations Follow the normal rules of linear algebra. c = a b Matrix multiplication: the number of columns in a must be equal to the number of rows in b! 1( , ) ( , ) ( , )nkc i j a i k b k j?? ?1234a??? ????1321b?????????5511 13ab??????????Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 58 Symbol: ?.‘ period Operation MATLAB Form Comments Array Addition a + b Array and matrix. Array Subtraction a – b Array and matrix. Array Multiplication a .* b Elementbyelement multiplication of a and b. Matrix Multiplication a * b Matrix multiplication of a and b. Array Right Division a ./ b Elementbyelement division of a and b : a (i, j) /b (i, j). Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 59 Operation MATLAB Form Comments Array Left Division a .\ b Elementbyelement division of b and a : b (i, j) / a (i, j). Matrix Right Division a / b Matrix division defined by a * inv(b). Matrix Left Division a \ b Matrix division defined by b * inv(a). Array Exponentiation a .^ b Elementbyelement exponentiation of a and b : a (i, j) ^ b (i, j). Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 60 What about Matrix Exponentiation? a / b == b \ a? Programming Pitfalls Be careful to distinguish between array operations and matrix operations, especially with square matrices. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 61 Example (a) a + b (b) a .* b (c) a * b (d) a * c (e) a + c (f) a + d (g) a .* d (h) a * d 1021a??? ????1201b????????32c???????5d?