0% found this document useful (0 votes)
5 views

Lab 2 Creating Arrays

This document provides an overview of creating and manipulating arrays in MATLAB, including one-dimensional arrays (vectors) and two-dimensional arrays (matrices). It explains how to create these arrays, address their elements, and perform operations such as adding or deleting elements. Additionally, it covers built-in functions for handling arrays and introduces the concept of strings as arrays of characters.

Uploaded by

Darren O'Brien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 2 Creating Arrays

This document provides an overview of creating and manipulating arrays in MATLAB, including one-dimensional arrays (vectors) and two-dimensional arrays (matrices). It explains how to create these arrays, address their elements, and perform operations such as adding or deleting elements. Additionally, it covers built-in functions for handling arrays and introduces the concept of strings as arrays of characters.

Uploaded by

Darren O'Brien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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.

2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)


A one-dimensional array is a list of numbers arranged in a row or a column. One example is
the representation of the position of a point in space in a three-dimensional Cartesian
coordinate system. As shown in Figure 2-1, the position of point A is defined by a list of the
three numbers 2, 4,and 5, which are the coordinates of the point.

The position of point A can be expressed in terms of a position vector:

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.

Figure 2-1: Position of a point.


TABLE 2-1: Population data

Year 1984 1986 1988 1990 1992 1994 1996


Population (millions) 127 130 136 145 158 178 211

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.

Creating a vector from a known list of numbers:


The vector is created by typing the elements (numbers) inside square brackets [ ].

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:

Some examples are:


• If the numbers m, q, and 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 does not exceed n.
• If only two numbers (the first and the last terms) are typed (the spacing is omitted),
then the default for the spacing is 1.

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.

Tutorial 2-2: Creating matrices.

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.

2.2.1 The zeros, ones and, eye Commands


The zeros(m,n), ones(m,n), and eye(n) commands can be used to create matrices that have
elements with special values. The zeros(m,n) and the ones(m,n) commands create a matrix
with m rows and n columns in which all elements are the numbers 0 and 1, respectively. The
eye(n) command creates a square matrix with n rows and n columns in which the diagonal
elements are equal to 1 and the rest of the elements are 0. This matrix is called the identity
matrix. Examples are:

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.4 THE TRANSPOSE OPERATOR


The transpose operator, when applied to a vector, switches a row (column) vector to a
column (row) vector. When applied to a matrix, it switches the rows (columns) to columns
(rows). The transpose operator is applied by typing a single quote ’ following the variable to
be transposed. Examples are:
2.5 ARRAY ADDRESSING
Elements in an array (either vector or matrix) can be addressed individually or in subgroups.
This is useful when there is a need to redefine only some of the elements, when specific
elements are to be used in calculations, or when a subgroup of the elements is used to
define a new variable.

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.

For example, if the matrix is:

then ma(1,1) = 3 and ma(2,3) = 10.

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:

2.6 USING A COLON : IN ADDRESSING ARRAYS


A colon can be used to address a range of elements in a vector or a matrix.

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.

Tutorial 2-3: Using a colon in addressing arrays.


In Tutorial 2-3 new vectors and matrices are created from existing ones by using a range of
elements, or a range of rows and columns (using :). It is possible, however, to select only
specific elements, or specific rows and columns of existing variables to create new variables.
This is done by typing the selected elements or rows or columns inside brackets, as shown
below:
2.7 ADDING ELEMENTS TO EXISTING VARIABLES
A variable that exists as a vector, or a matrix, can be changed by adding elements to it
(remember that a scalar is a vector with one element). A vector (a matrix with a single row
or column) can be changed to have more elements, or it can be changed to be a two-
dimensional matrix. Rows and/orcolumns can also be added to an existing matrix to obtain
a matrix of different size. The addition of elements can be done by simply assigning values
to the additional elements, or by appending existing variables.

Adding elements to a vector:


Elements can be added to an existing vector by assigning values to the new elements. For
example,if a vector has 4 elements, the vector can be made longer by assigning values to
elements 5, 6, and so on. If a vector has n elements and a new value is assigned to an
element with an address of n+2 or larger, MATLAB assigns zeros to the elements that are
between the last original element and the new element. Examples:

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:

2.8 DELETING ELEMENTS


An element, or a range of elements, of an existing variable can be deleted by reassigning
nothing to these elements. This is done by using square brackets with nothing typed in
between them. By deleting elements, a vector can be made shorter and a matrix can be
made smaller. Examples are:
2.9 BUILT-IN FUNCTIONS FOR HANDLING ARRAYS
MATLAB has many built-in functions for managing and handling arrays. Some of these are
listed below:
TABLE 2-2: Built-in functions for handling arrays
Additional built-in functions for manipulation of arrays are described in the Help Window. In
this window, select “MATLAB,” then in the Contents “Functions,” and then “By Category.”

Sample Problem 2-1: Create a matrix


Using the ones and zeros commands, create a 4 × 5 matrix in which the first two rows are 0s
and the next two rows are 1s.

Solution

A different solution to the problem is:


Sample Problem 2-2: Create a matrix
Create a 6 × 6 matrix in which the middle two rows and the middle two columns are 1s and
the rest of the entries are 0s.

Solution

Sample Problem 2-3: Matrix manipulation


Given are a 5 × 6 matrix A, a 3 × 6 matrix B, and a 9-element vector v.
Create the three arrays in the Command Window, and then, by writing one
command, replacethe last four columns of the first and third rows of A with
the first four columns of the first two rows of B, the last four columns of the
fourth row of A with the elements 5 through 8 of v, andthe last four columns
of the fifth row of A with columns 3 through 5 of the third row of B.

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.

You might also like