Lab 2 Creating Arrays
Lab 2 Creating Arrays
The array is a fundamental form that MATLAB uses to store and manipulate data. 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 inrows 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 two-dimensional arrays often represent matrices. This lab shows how to create
and address arrays, and Lab 3 shows how to use arrays in mathematical operations. In
addition to arrays made of numbers, arrays in MATLAB can also be a list of characters,
which are called strings. Strings are discussed in Section 2.10.
where i, j, and k are unit vectors in the direction of the x, y, and z axes, respectively. The
numbers 2,4, and 5 can be used to define a row or a column vector.
Any list of numbers can be set up as a vector. For example, Table 2-1 contains population
growth data that can be used to create two lists of numbers—one of the years and the other
of the population values. Each list can be entered as elements in a vector with the numbers
placed in a row or in a column.
In MATLAB, a vector is created by assigning the elements of the vector to a variable. This
can be done in several ways depending on the source of the information that is used for the
elements of the vector. When a vector contains specific numbers that are known (like the
coordinates of point A), the value of each element is entered directly. Each element can also
be a mathematical expression that can include predefined variables, numbers, and
functions. Often, the elements of a row vector are a series of numbers with constant
spacing. In such cases the vector can be created with MATLAB commands. A vector can
also be created as the result of mathematical operations as explained in Lab 3.
Row vector: To create a row vector type the elements with a space or a comma between
the elements inside the square brackets.
Column vector: To create a column vector type the left square bracket [ and then enter the
elements with a semicolon between them, or press the Enter key after each element. Type
the right square bracket ] after the last element.
Tutorial 2-1 shows how the data from Table 2-1 and the coordinates of point A are used to
create row and column vectors.
Tutorial 2-1: Creating vectors from given data.
Creating a vector with constant spacing by specifying the first term, the spacing,
and the last term:
In a vector with constant spacing, the difference between the elements is the same. For
example, in the vector 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:
Creating a vector with linear (equal) spacing by specifying the first and last terms,
and the number of terms:
A vector with n elements that are linearly (equally) spaced in which the first element is xi and
the last element is xf can be created by typing the linspace command (MATLAB determines the
correct spacing):
When the number of elements is omitted, the default is 100. Some examples are:
2.2 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 the arrangement in a table. Matrices play an important
role in linear algebra and are used in science and engineering to describe many physical
quantities.
In a square matrix the number of rows and the number of columns is equal. For example, the
matrix
is square, with three rows and three columns. In general, the number of rows and columns
can be different. For example, the matrix:
has four rows and six columns. A m × n matrix has m rows and n columns, and m by n is
called the size of the matrix.
A matrix is created by assigning the elements of the matrix to a variable. This is done by
typing the elements, row by row, inside square brackets [ ]. First type the left bracket [ then
type the first row, separating the elements with spaces or commas. To type the next row type
a semicolon or press Enter. Type the right bracket ] at the end of the last row.
The elements that are entered can be numbers or mathematical expressions that may
include numbers, predefined variables, and functions. All the rows must have the same
number of elements.If an element is zero, it has to be entered as such. MATLAB displays an
error message if an attempt is made to define an incomplete matrix. Examples of matrices
defined in different ways are shown in Tutorial 2-2.
Rows of a matrix can also be entered as vectors using the notation for creating vectors with
constant spacing, or the linspace command. For example:
In this example the first two rows were entered as vectors using the notation of constant
spacing, the third row was entered using the linspace command, and in the last row the
elements were entered individually.
Matrices can also be created as a result of mathematical operations with vectors and
matrices. This topic is covered in Lab 3.
2.3 NOTES ABOUT VARIABLES IN MATLAB
• All variables in MATLAB are arrays. A scalar is an array with one element, a vector
is an array with one row or one column of elements, and a matrix is an array with
elements in rows and columns.
• The variable (scalar, vector, or matrix) is defined by the input when the variable is
assigned. There is no need to define the size of the array (single element for a
scalar, a row or a column of elements for a vector, or a two-dimensional array of
elements for a matrix) before the elements are assigned.
• Once a variable exists—as a scalar, vector, or matrix—it can be changed to any
other size, or type, of variable. For example, a scalar can be changed to a vector
or a matrix; a vector can be changed to a scalar, a vector of different length, or a
matrix; and a matrix can be changed to have a different size, or be reduced to a
vector or a scalar. These changes are made by adding or deleting elements. This
subject is covered in Sections 2.7 and 2.8.
2.5.1 Vector
The address of an element in a vector is its position in the row (or column). For a vector
named ve, ve(k) refers to the element in position k. The first position is 1. For example, if the
vector ve has nine elements:
then
A single vector element, v(k), can be used just as a variable. For example, it is possible to
change the value of only one element of a vector by assigning a new value to a specific
address. This is done by typing: v(k) = value. A single element can also be used as a
variable in a mathematical expression. Examples are:
2.5.2 Matrix
The address of an element in a matrix is its position, defined by the row number and the
column number where it is located. For a matrix assigned to a variable ma, ma(k,p) refers to
the element in row k and column p.
As with vectors, it is possible to change the value of just one element of a matrix by
assigning a new value to that element. Also, single elements can be used like variables in
mathematical expressionsand functions. Some examples are:
For a vector:
va(:) Refers to all the elements of the vector va (either a row or a column vector).
va(m:n) Refers to elements m through n of the vector va.
Example:
For a matrix:
A(:,n) Refers to the elements in all the rows of column n of the matrix A.
A(n,:) Refers to the elements in all the columns of row n of the matrix A.
A(:,m:n) Refers to the elements in all the rows between columns m and n of the
matrix A.
A(m:n,:) Refers to the elements in all the columns between rows m and n of the
matrix A.
A(m:n,p:q) Refers to the elements in rows m through n and columns p through q of the
matrix A.
The use of the colon symbol in addressing elements of matrices is demonstrated in Tutorial
2-3.
Elements can also be added to a vector by appending existing vectors. Two examples are:
Adding elements to a matrix:
Rows and/or columns can be added to an existing matrix by assigning values to the new
rows or columns. This can be done by assigning new values, or by appending existing
variables. This must be done carefully since the size of the added rows or columns must fit
the existing matrix. Examples are:
If a matrix has a size of m × n and a new value is assigned to an element with an address
beyond the size of the matrix, MATLAB increases the size of the matrix to include the new
element. Zeros are assigned to the other elements that are added. Examples:
Solution
Solution
Solution
2.10 STRINGS AND STRINGS AS VARIABLES
• A string is an array of characters. It is created by typing the characters within single
quotes.Strings can include letters, digits, other symbols, and spaces.
• Examples of strings: 'ad ef ', '3%fr2', '{edcba:21!', 'MATLAB'.
• A string that contains a single quote is created by typing two single quotes within
the string.
• When a string is being typed in, the color of the text on the screen changes to
maroon when thefirst single quote is typed. When the single quote at the end of the
string is typed, the color of thestring changes to purple.
Strings have several different uses in MATLAB. They are used in output commands to
display text messages (Lab 4), in formatting commands of plots (Lab 5), and as input
arguments of some functions (Lab 7). More details are given in these labs when strings are
used for these purposes.
• When strings are being used in formatting plots (labels to axes, title, and text
notes), characters within the string can be formatted to have a specified font, size,
position (uppercase, lowercase),color, etc. See Lab 5 for details.
Strings can also be assigned to variables by simply typing the string on the right side of the
assignment operator, as shown in the examples below:
When a variable is defined as a string, the characters of the string are stored in an array just
as numbers are. Each character, including a space, is an element in the array. This means
that a one-line string is a row vector in which the number of elements is equal to the number
of characters. The elements of the vectors are addressed by position. For example, in the
vector B that was defined above the 4th element is the letter n, the 12th element is J, and so
on.
As with a vector that contains numbers, it is also possible to change specific elements by
addressing them directly. For example, in the vector B above the name John can be changed
to Bill by:
Strings can also be placed in a matrix. As with numbers, this is done by typing a semicolon ;
(or pressing the Enter key) at the end of each row. Each row must be typed as a string,
which means that it must be enclosed in single quotes. In addition, as with a numerical
matrix, all rows must have the same number of elements. This requirement can cause
problems when the intention is to create rows with specific wording. Rows can be made to
have the same number of elements by adding spaces.
MATLAB has a built-in function named char that creates an array with rows having the same
number of characters from an input of rows not all of the same length. MATLAB makes the
length of all the rows equal to that of the longest row by adding spaces at the end of the
short lines. In the char function, the rows are entered as strings separated by a comma
according to the following format:
For example:
A variable can be defined as either a number or a string made up of the same digits. For
example, as shown below, x is defined to be the number 536, and y is defined to be a string
made up of the digits 536.
The two variables are not the same even though they appear identical on the screen. Note
that the characters 536 in the line below the x= are indented, while the characters 536 in the
line below the y= are not indented. The variable x can be used in mathematical expressions,
whereas the variable y cannot.