1 / 43

MATLAB – Ch 2 - N umeric, Cell, & Structure Arrays

MATLAB – Ch 2 - N umeric, Cell, & Structure Arrays. EGR1302. Outline. Introduction Arrays Multidimensional arrays Element-by-element operations Polynomial operations using arrays. Introduction. Array capabilities in Matlab Serves as basic building block in Matlab

eitan
Download Presentation

MATLAB – Ch 2 - N umeric, Cell, & Structure Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB – Ch 2 - Numeric, Cell, & Structure Arrays EGR1302

  2. Outline • Introduction • Arrays • Multidimensional arrays • Element-by-element operations • Polynomial operations using arrays

  3. Introduction • Array capabilities in Matlab • Serves as basic building block in Matlab • Allows for complex operations using one command or function • Means that Matlab programs can be very short

  4. Introduction • Arrays in Chapter 1 • Array assignment • “[]” contains numbers being assigned • Commas or spaces separate elements in row • Semi-colons separate rows • Special format for assigning row array with regularly spaced numbers

  5. Introduction • Arrays in Chapter 1 • Use just variable name in expressions • Operation is performed on every element in the array • Use variable name & array index in expressions • Use length function to determine number of elements in an array

  6. Section 2.1 Arrays

  7. Cartesian coordinates review • Represent a point p in 3D space • x, y, & z • Represent unit vectors

  8. Cartesian coordinates review • Represent a vector p from origin to point p • In Matlab • Row vector • Column vector

  9. Creating vectors in Matlab • To create a row vector • Type numbers within square brackets • Separate numbers with a space or a comma >>p = [3,7,9] p = 3 7 9

  10. Creating vectors in Matlab • To create a column vector • Type numbers within square brackets • Separate numbers with a semi-colon • OR transpose a row vector >>p = [3;7;9] p = 3 7 9 >>p = [3,7,9]’

  11. Appending one vector to another • Append 2 row vectors >> r = [2,4,20]; >> w = [9,-6,3]; >> u = [r,w] u = 2 4 20 9 -6 3

  12. Generating vectors of regularly spaced elements • Use colon “:” operator • x1 = first value in the series • x2 = last value in the series • d = increment between numbers in series (default is 1 if d is omitted) • Use linspace function • n =number of points (default is 1 if n is omitted)

  13. Generating vectors of regularly spaced elements • Use logspace function • a = exponent for first value (i.e., 10a) • b = exponent for last value (i.e., 10b) • n =number of points (default is 50 if n is omitted)

  14. 2D Arrays • Array • Collection of scalars arranged in logical manner • Row vector • Array with a single row of scalars • Column vector • Array with a single column of scalars • Matrix • An array with multiple rows and columns

  15. 2D Arrays • Square brackets denote matrices • Recall  Parallel lines denote a determinant

  16. Creating Matrices • Type row by row • Semi-colon separating rows • Comma or space separating elements in a row >> A = [2,4,10;16,3,7] A = 2 4 10 16 3 7

  17. Array addressing • v(5)  5th element in vector v • A(2,3)  Element in 2nd row and 3rd column in array A • Row number is always first! • D(1,3) = 4 • Replaces the element in 1st row, 3rd column of array D with 4

  18. Array addressing • Colon operator • Selects submatrices • v(:)  all row or column elements in vector v • v(2:4)  2nd through 4th elements in v • A(3,:)  all elements in the 3rd row of matrix A • A( :,2:4)  all elements in the 2nd through 4th columns of A • B(2:4,1:3)  all elements in the 2nd through 4th rows and 1st through 3rd columns of B

  19. Useful array functions • See Table 2.1-1, p. 77 • Max • For a vector x, returns algebraically greatest element • For a matrix B, returns • Row vector x containing greatest element in each column of B • Row vector k containing indices of greatest elements in each column of B >> y = max(x) >> [x,k] = max(B)

  20. Vector terms • Length • Number of elements in a vector • Magnitude • Vector’s geometric length • Absolute value • Absolute values of each elements in vector

  21. Array editor • Graphical interface for working with arrays • View and edit workspace variables • Clear workspace variables • Plotting workspace variables

  22. Section 2.2 Multidimensional arrays

  23. 3D & 4D arrays • 1st dimension is row • 2nd dimension is column • Higher dimensions are referred to as pages

  24. Section 2.3 Element-by-element operations

  25. Scalar multiplication • Increase magnitude of a vector by multiplying it by scalar r=[3,5,2]; >> v=2*r v = 6 10 4

  26. Array addition & subtraction • 2 arrays with same size • Sum or difference has same size • Add or subtract corresponding elements >> A=[6,-2;10,3]; >> B=[9,8;-12,14]; >> C=A+B C = 15 6 -2 17

  27. Array addition & subtraction • Associative • Commutative

  28. Multiplication of two arrays • Two definitions of multiplication of two arrays • Array multiplication • Element-by-element operation • Matrix multiplication • Division and exponentiation also must be carefully defined

  29. Table 2.3-1, p.85 Symbol + - + - .* ./ .\ .^ 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 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]=[2\4,5\8] [3,5].^2=[3^2,5^2] 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4]

  30. Array multiplication • Vectors must be of the same size • Matrices must be of the same size • Dot (.) and asterisk (*) form one symbol

  31. Built-in Matlab functions • sqrt(x) and exp(x) • Automatically operate on array arguments to produce an array result the same size as the array argument. • Thus these functions are said to be vectorizedfunctions

  32. Built-in Matlab functions • When multiplying, dividing, or exponentiating these functions, you must use element-by-element operations if the arguments are arrays. • To compute z = (ey sin x) cos2x, you must type

  33. Section 2.4 Matrix operations

  34. Addition & Subtraction • Matrix addition and subtraction are identical to element-by-element addition and subtracted

  35. Vector Multiplication • Vector dot product • Recall  result is a scalar

  36. Vector-Matrix Multiplication • Matrix multiplied by column vector • Result is a column vector • Number of columns in matrix must equal number of rows in vector

  37. Matrix multiplication • To multiply two matrices A & B • Number of columns in A must equal number of rows in • The resulting product AB has • Same number of rows as A • Same number of columns as B 6 –2 10 3 4 7 (6)(9) + (– 2)(– 5) (6)(8) + (– 2)(12) (10)(9) + (3)(– 5) (10)(8) + (3)(12) (4)(9) + (7)(– 5) (4)(8) + (7)(12) 9 8 –5 12 = 64 24 75 116 1 116 =

  38. Matrix multiplication • Matrix multiplication is NOT commutative • The order of the matrices in the equation is important • Exceptions • Null matrix 0 • Identity matrix I

  39. Section 2.5 Polynomial operations using arrays

  40. Polynomials in Matlab • Defined as row vector • Containing coefficients • Starting with coefficient of highest power of x • Addition & subtraction • Add row vectors • BUT if polynomials are of different degrees, add zeros to coefficient array of lower degree polynomial • Fool Matlab into thinking that the lower degree polynomial has the same degree

  41. Polynomial functions • Roots(a)  calculate roots • Poly(a)  computes coefficients of polynomial whose roots are contained in a • See Table 2.5-1 on p.108 for additional functions

  42. Plotting a polynomial • Function polyval(a,x) • Evaluates a polynomial at specified values of its independent variable x, which can be a matrix or a vector. • The polynomial’s coefficients of descending powers are stored in the array a. • The result is the same size as x.

More Related