0% found this document useful (0 votes)
63 views4 pages

Ch2: Arrays and Matrices: Reference

The document discusses arrays and matrices in MATLAB. It defines what arrays and matrices are, how to create vectors, row and column vectors, matrices with constant and linear spacing. It also discusses commands to build arrays and matrices like zeros, ones, and eye and how to refer to and modify matrix elements.
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)
63 views4 pages

Ch2: Arrays and Matrices: Reference

The document discusses arrays and matrices in MATLAB. It defines what arrays and matrices are, how to create vectors, row and column vectors, matrices with constant and linear spacing. It also discusses commands to build arrays and matrices like zeros, ones, and eye and how to refer to and modify matrix elements.
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/ 4

MATLAB Lecture ch2: Arrays and Matrices Dr.Hazim G.

Dway

Ch2: Arrays and Matrices


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 in rows and columns. One use of
arrays is to store information and data.
Reference
1. Amos Gilat, "MATLAB® An Introduction with Applications", John Wiley & Sons,
Inc.2011.
2.Stormy Attaway, "Matlab:A Practical Introduction to Programming and
ProblemSolving",Elsevier, Inc,2009.
3. Tobin A. Driscoll ,"Learning MATLAB",SIAM,2009.
2.1. Creating Vector and Matrix

Vectors and matrices are used to store sets of values, all of which are the same
type. A vector can be either a row vector or a column vector. A matrix can be
visualized as a table of values. The dimensions of a matrix are r × c, where r is the
number of rows and c is the number of columns. This is pronounced “r by c.” If a
vector has n elements, a row vector would have the dimensions 1 × n, and a column
vector would have the dimensions n × 1. A scalar (one value) has the dimensions 1 ×
1. Therefore, vectors and scalars are actually just subsets of matrices. Here are some
diagrams showing, from left to right, a scalar, a column vector, a row vector, and a
matrix:

The scalar is 1 × 1, the column vector is 3 × 1 (3 rows by 1 column), the row vector is
1 × 4 (1 row by 4 columns), and the matrix is 3 × 3. All the values stored in these
matrices are stored in what are called elements.

2.1.1 Creating Row and Column Vectors

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. For example:

1
MATLAB Lecture ch2: Arrays and Matrices Dr.Hazim G. Dway

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 .For example:

To (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:

To (Creating a vector with linear or 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
MATLAB Lecture ch2: Arrays and Matrices Dr.Hazim G. Dway

2.1.2 Creating a Two-Dimensional Array (matrix)


A two-dimensional array, also called a matrix, has numbers in rows and columns. 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.

For example:

3
MATLAB Lecture ch2: Arrays and Matrices Dr.Hazim G. Dway

2.2 Commands for Building Arrays and Matrices

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. 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.

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:

2.2.2 Referring to and Modifying Matrix Elements

To refer to matrix elements, the row and then the column indices are given in
parentheses (always the row index first and then the column). For example, this
creates a matrix variable mat, and then refers to the value in the second row, third
column of mat as examples:

You might also like