Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.

Similar presentations


Presentation on theme: "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices."— Presentation transcript:

1 ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices Creating Arrays / Chapter 2

2 ENG 1181 2 Arrays: Vectors and Matrices 27- 28 The array is the fundamental form MATLAB uses to store data Scalars – one row and one column (special case) Vectors Row vector – one row and multiple columns Column vector – multiple rows and one column Matrices – multiple rows and multiple columns

3 ENG 1181 3 Arrays: Example Arrays are used in many applications.  They can represent data: An example is a position vector. The location of point P in a three dimensional space can be represented by the three Cartesian coordinates 2, 4, and 5.  Arrays of numbers can represent a vector. 27- 28 x y z 2 4 5 P (2, 4, 5) rArA O Year1984198619881990199219941996 Population127130136145158178211

4 ENG 1181 4 The year and population data in the previous slide can be entered as vectors in rows: Year = [1984 1986 1988 1990 1992 1994 1996] Population = [127 130 136 145 158 178 211] 27- 28 Vectors or in columns:

5 ENG 1181 5 CREATING A ROW VECTOR IN MATLAB A vector can be created by typing the elements (numbers) inside square brackets [ ] separated by a comma or space(s). 28- 29 Type and press Enter >> pntAH = [2,4,5] pntAH = 2 4 5 Computer Response

6 ENG 1181 6 COLUMN VECTORS >> pop = [127; 130; 136; 145; 158; 178; 211] pop = 127 130 136 145 158 178 211 >> pntAV = [2 4 5] pntAV = 2 4 5 28- 29 Two methods: Separate elements by semicolon or “enter key”.

7 ENG 1181 7 CREATING A VECTOR WITH CONSTANT SPACING A vector in which the first term is m, the spacing is q and the last term is n can be created by typing [m : q : n]. >> x = [1 : 2 : 13] or x = 1 : 2 : 13 x = 1 3 5 7 9 11 13 >> x = [1.5 : 0.1 : 2.1] x = 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 If spacing (q) is omitted the default is 1 >> x = [-3 : 7] x = -3 -2 -1 0 1 2 3 4 5 6 7 29- 30

8 ENG 1181 8 An alternate method Sometimes you know the number of terms and not the spacing – for this case, the function linspace is useful –Specify first term: step size: last term –linspace (first term, last term, number of terms)

9 ENG 1181 9 TWO DIMENSIONAL ARRAY - MATRIX The number of rows and columns may be the same or different. 31 26 14 18 3 51 20 11 Two rows and four columns (2 x 4) 30- 32 A (m x n), or “m by n”, matrix has m rows and n columns. (m x n) is called the size of the matrix

10 ENG 1181 10 CREATING A MATRIX IN MATLAB A Matrix is created by typing the elements (numbers) row by row inside square brackets [ ]. Two methods are shown, with and without the semicolon. 30- 32 >> a = [5 35 43; 4 76 81; 21 32 40] a = 5 35 43 4 76 81 21 32 40 >> b = [7 2 76 33 8 1 98 6 25 6 5 54 68 9 0] b = 7 2 76 33 8 1 98 6 25 6 5 54 68 9 0 Type and press Enter Computer Response Type and press Enter After each row and after the ] Computer Response

11 ENG 1181 11 >> a = 7 a = 7 >> E = 3 E = 3 >> d = [5 a+E 4 E^2] d = 5 10 4 9 >> g = [a a^2 13; a*E 1 a^E] g = 7 49 13 21 1 343 ARRAY EXAMPLES 43

12 ENG 1181 12 THE TRANSPOSE OPERATION The transpose operation ' (single quote) For a vector: Converts a row vector to a column vector, or vice versa. For a matrix: Interchanges the rows and columns. 33- 34

13 ENG 1181 13 Vector transpose >> a = [3 8 1] a = 3 8 1 >> b = a' b = 3 8 1 >> c = 1:2:5 c = 1 3 5 >> d = c' d = 1 3 5

14 ENG 1181 14 MATRIX TRANSPOSE Matrix example: >> c = [1 2 3 4; 5 5 5 5; 6 7 8 9] c = 1 2 3 4 5 5 5 5 6 7 8 9 >> d = c' d = 1 5 6 2 5 7 3 5 8 4 5 9 33- 34


Download ppt "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices."

Similar presentations


Ads by Google