Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Arrays in MATLAB

Similar presentations


Presentation on theme: "Working with Arrays in MATLAB"— Presentation transcript:

1 Working with Arrays in MATLAB
Chapter 3 MATLAB Fundamentals Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2 Review: Scalars and Arrays
In a spreadsheet, we do not need to name every variable: the cell address (row/column) defines the storage location Storage locations for time and displacement values Engineering Computation: An Introduction Using MATLAB and Excel

3 Arrays In MATLAB and other programming languages, the variables must be named and storage of results must be planned An array is a variable that has multiple values An array’s name is followed by an index (or indices, for multi-dimensional arrays) in parentheses Engineering Computation: An Introduction Using MATLAB and Excel

4 Indices of Arrays Think of the index value as an address
Important: Indices must be positive integers, and must start with one Example: To store these results, we would define t(1) = 0, t(2) = 0.1, t(3) = 0.2… y(1) = 3, y(2) = … Engineering Computation: An Introduction Using MATLAB and Excel

5 Common Mistakes Using Indices with MATLAB
Using an index of zero: Using an index not an integer: setting t(0.1) equal to a value results in the same error as above Engineering Computation: An Introduction Using MATLAB and Excel

6 Common Mistakes Using Indices with MATLAB
Progressing indices by values other than 1. For example, if we take a temperature reading every 10 seconds, and set the first value as T(10), then zeros are filled in for the first nine values of T: (No error is reported, but this result is probably not what is desired) All of these errors result from confusion between independent variables and indices. Thinking of indices as addresses helps avoid these errors Engineering Computation: An Introduction Using MATLAB and Excel

7 Multi-Dimensional Arrays
A variable may have more than one index. A two-dimensional array is similar to the data storage of a spreadsheet. Instead of a row number and a column letter, a two-dimensional array has two index numbers Examples: t(1,5) R(3,3) temp(1,20) Efficient way to store data: if both indices range from 1-10, then the number of values stored is X 10 = 100 Engineering Computation: An Introduction Using MATLAB and Excel

8 Matrices One- and two-dimensional arrays are also called matrices
Many mathematical operations can be performed with matrices - the name MATLAB comes from “Matrix Laboratory” The size of a matrix is defined by the numbers of rows and columns. Example; a 3 X 2 matrix: Engineering Computation: An Introduction Using MATLAB and Excel

9 Vectors One-dimensional matrices are referred to in MATLAB as vectors
Vectors can be classified as column vectors (all values arranged in a single column) or row vectors (all values arranged in a single row) Caution: in mechanics classes, the term “vector” has a different definition: a vector is defined as a physical quantity which is defined by magnitude and direction Engineering Computation: An Introduction Using MATLAB and Excel

10 Entering Arrays in MATLAB
Consider the matrix: To enter this matrix in MATLAB: or Commas or spaces separate elements within a row, semi-colons separate rows Engineering Computation: An Introduction Using MATLAB and Excel

11 Arrays We will work with matrix math later, with applications such as solving simultaneous equations We will create large arrays of results using programming loops. Loops control the execution of repetitive calculations, and the results are often stored in arrays Engineering Computation: An Introduction Using MATLAB and Excel


Download ppt "Working with Arrays in MATLAB"

Similar presentations


Ads by Google