ENGR 1187 MATLAB 2: Array Creationpreparation Material

ENGR 1187 MATLAB 2: Array Creationpreparation Material

<p>ENGR 1187 | MATLAB 2: Array Creation Preparation Material</p><p>Learning Objectives 1. Demonstrate proper convention for assigning arrays to a variable (e.g., space, semi-colons, colon operators, linspace function)</p><p>2. Apply the transpose operator correctly</p><p>Topics Read Chapter 2.1 – 2.4 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading. Students will learn basic understanding of arrays and how to create them (vectors and matrices) in MATLAB. This material contains the following:</p><p>1. Key Definitions & Ideas</p><p>2. Vectors </p><p>3. Matrices </p><p>4. Transpose</p><p>5. Examples</p><p>1. Key Definitions & Ideas Arrays – An array is a data structure that contains a group of elements of the same data type. An array can be a scalar, vector or matrix (explained below). The size of arrays are commonly described by the number of rows and columns they contain. This is explained in further detail below.</p><p>Scalar – A scalar variable is a variable with 1 row and 1 column. For example, type the following line in the MATLAB command window:  >> x = 5 The variable x is a scalar variable (contains a single value) and has only 1 row and 1 column.</p><p>Vector – A vector is a string of values (or a string of scalars). There are two types of vectors you can create in MATLAB. They are:</p><p> Row vector – one row and multiple columns</p><p> Column vector – multiple rows and one column</p><p>Matrix – A matrix is a rectangular array of numbers, otherwise described as having multiple rows and multiple columns. </p><p>1 ENGR 1181 MATLAB 2: Array Creation </p><p>Preparation Material 2. Vectors A vector in MATLAB is an array that contains multiple elements in either a single row or a single column. Vectors are commonly referred to as one-dimensional arrays.</p><p>Vectors are useful for many applications (e.g., representing position of an object, scientific data, etc…). </p><p>Table 1: Population Growth Data Year 1984 1986 1988 1990 1992 1994 1996</p><p>Population 127 130 136 145 158 178 211</p><p>Creating vectors: In MATLAB, a row vector can be created by typing the elements (numbers) inside of square brackets [ ], separating them by a comma or space(s). For example, we can create two row vectors of the data presented in Table 1. Type the following line in the MATLAB command window:</p><p>You’ll notice that both methods of creating a row vector produce the same output (no comma vs. comma).</p><p>To create column vectors, semi-colons can be used in place of the comma or if the data is entered into a script file the ‘Enter’ key can be used. For example, type the following in the command window</p><p>Or in a script file the vector can be created as follows: year = [ 1984 1986 1988 1990 1992 1994 1996]</p><p>Vectors with equal spacing: A vector in which the starting value is m, the spacing is q and the last value is n can be created by typing [m : q : n]. This can be described as [ start : spacing : end ]. ENGR 1181 MATLAB 2: Array Creation </p><p>Preparation Material</p><p>If spacing (q) is omitted, then it is simply [m : n] and the default step is 1. This can be described as [ start : end ]. </p><p>Alternatively, the command linspace() can be used to properly space the variables in a vector. This is useful when the spacing is difficult to identify, but the number of needed data points is known. This is written as linspace( m , n , r ), where the first term is m, the last term is n, and the number of data points is r.</p><p>Example: Let’s say someone knows the coordinates on a grid and wants to evenly space 7 items from location 2.5 to 14.5. He/she may not know the exact spacing, especially if design plans change the number of poles is reduced to 6 or 8. To save time and errors, the designer can use linspace() the following way:</p><p>Note that calculating (14.5-2.5)/7 = 1.714286 doesn’t work properly because the end points are included in linspace(). The proper math would actually be (14.5-2.5)/6 or referenced as (n – m) / (r - 1).</p><p>3. Matrices A (m x n), or “m by n”, matrix has m rows and n columns. The number of rows and columns may be the same or different. (m x n) is called the size of the matrix.</p><p>The following matrix G has two rows and four columns (2 x 4), or “2 by 4”</p><p>A matrix is created by typing the elements (numbers) row by row inside square brackets [ ]. Similar to a column vector, the multiple rows in a matrix can be created with a semicolon or the ‘Enter’ key. ENGR 1181 MATLAB 2: Array Creation </p><p>Preparation Material 4. Transpose The transpose operation is executed by adding an apostrophe (’) after the vector or matrix:</p><p>For a vector: Converts a row vector to a column vector, or converts a column vector to a row vector. </p><p>For a matrix: Interchanges the rows and columns. For example, a 2 x 4 matrix becomes a 4 x 2 matrix. ENGR 1181 MATLAB 2: Array Creation </p><p>Preparation Material 5. Array Examples</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us