MATLAB
MATLAB
Window Purpose
4
Creating Arrays
• An array is a list of numbers arranged in rows and /or columns
• The simplest array, (one-dimensional) is a row or a column of
numbers
• A more complex array (two-dimensional) is a collection of
numbers arranged in rows and columns
• One use of arrays is to store information and data, as in a table
• In science and engineering, one-dimensional arrays frequently
represent vectors and 2-D arrays often represent matrices
• In addition to arrays that are made of numbers, arrays in
MATLAB can also be made of a list of characters, which are
called strings
5
• A 1-D array is a list of numbers that
is placed in a row or a column
• One example is the representation of
the position of a point in space in a
3-D Cartesian coordinate system
• The position of point A is defined
by a list of 3 numbers 2, 4, and 5,
which are coordinates of the point where i, j, k are
• The position of point A can be unit vectors in the
expressed in terms of a position direction of the x,
y, and z axis
vector:
rA= 2i + 4j + 5k respectively
Creating a vector from a known list of
numbers
Year 1984 1986 1988 1990 1992 1994 1996
Population 127 130 136 145 158 178 211
(Millions)
V = 2 4 6 8 10
The spacing between the elements is 2
• A vector in which the first term is m, the spacing is q, and the last term is n is created
by typing
Variable_name = [m : q : n] or
Variable_name = m : q : n (brackets are optional)
• Consider these
x = [1 : 2 : 13]
y = [1.5 : 0.1 : 2.1] or
z = [-3 : 7] if spacing is omitted, the default is 1
xa = [21 : - 3 : 6]
• If the numbers m, q, n are such that the value of n cannot be obtained by adding q’s
to m, then ( for positive n) the last element in the vector will be the last number that
8
does not exceed n
Creating a vector with constant spacing by
specifying the first and last terms, and the number
of terms
• A vector in which the first element is xi, the last element is xf, and the
number of elements is n is created by typing the linspace command
(MATLAB determines the correct spacing)
• Examples are
va = linspace (0,8,6)
vb = linspace (30,10,11)
u = linspace (49.5, 0.5) (when the number of elements
is omitted, the default is 100)
CREATING A Two-DIMENSIONAL
ARRAY (MATRIX)
• A two-dimensional array, also called a matrix, has numbers in rows
and columns.
• Matrices can be used to store information like in a table.
• Matrices play an important role in linear algebra and are used in
science and engineering-to describe many physical quantities.
• A m x n matrix has m rows and n columns, and m by n is called the
size of the matrix.