Chapter 3: Arrays

advertisement
Working with Arrays in MATLAB
Chapter 3
MATLAB Fundamentals
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
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
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) = 2.45016…
Engineering Computation: An Introduction Using MATLAB and Excel
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
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
Multi-Dimensional Arrays
• A variable may have more than one index. A twodimensional 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
10 X 10 = 100
Engineering Computation: An Introduction Using MATLAB and Excel
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
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
Entering Arrays in MATLAB
• Consider the matrix:
• To enter this matrix in MATLAB:
or
Commas or spaces
separate elements
within a row, semicolons separate
rows
Engineering Computation: An Introduction Using MATLAB and Excel
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