The Matrices
Introduction
Matrices are the basic elements of the MATLAB environment. A matrix
is a two-dimensional array consisting of m rows and n columns. Special
cases are column vectors (n = 1) and row vectors (m = 1).
In this section we will illustrate how to apply different operations on
matrices. The following topics are discussed: vectors and matrices in
MATLAB, the inverse of a matrix, determinants, and matrix
manipulation.
MATLAB supports two types of operations, known as matrix
operations and array operations. Matrix operations will be discussed
first.
Matrix generation Matrices can be generated in several ways.
1- Entering a vector
A vector is a special case of a matrix. The purpose of this section is to
show how to create vectors and matrices in MATLAB. As discussed
earlier, an array of dimension 1*n is called a row vector, whereas an
array of dimension m£ 1 is called a column vector. The elements of
vectors in MATLAB are enclosed by square brackets and are separated
by spaces or by commas. For example, to enter a row vector, v, type
Column vectors are created in a similar way, however, semicolon (;)
must separate the components of a column vector,
On the other hand, a row vector is converted to a column vector using the transpose
operator. The transpose operation is denoted by an apostrophe or a single quote (').
Thus, v(1) is the ¯rst element of vector v, v(2) its second element, and so
forth. Furthermore, to access blocks of elements, we use MATLAB's
colon notation (:). For example, to access the first three elements of v,
we write,
If v is a vector, writing
>> v(:)
ans =
1
4
7
13
produces a column vector, whereas writing
>> v(1:end)
produces a row vector.
2- Entering a matrix:
A matrix is an array of numbers. To type a matrix into MATLAB you
must:
Here is a typical example. To enter a matrix A, such as,
type,
>> A = [1 2 3; 4 5 6; 7 8 9]
3- Matrix indexing :
Thus, A(i,j) in MATLAB refers to the element Aij of matrix A. The first
index is the row number and the second index is the column number.
For example, A(1,3) is an element of first row and third column. Here,
A(1,3)=3.
Correcting any entry is easy through indexing. Here we substitute
A(3,3)=9 by
A(3,3)=0. The result is
Single elements of a matrix are accessed as A(i,j), where i ¸ 1 and j ¸ 1. Zero or
negative subscripts are not supported in MATLAB.
4- Colon operator :
Often we must deal with matrices or vectors that are too large to enter one element
at a time. For example, suppose we want to enter a vector x consisting of points (0;
0:1; 0:2; 0:3; …….. ; 5). We can use the command
>> x = 0:0.1:5
Final number
Increment
X=
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000
st
1 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000
0.9000
number
1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000
2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000
3.6000 3.7000 3.8000 3.9000 4.0000 4.1000 4.2000 4.3000 4.4000
4.5000 4.6000 4.7000 4.8000 4.9000 5.0000 Note: The row vector has 51 elements.