Lecture 3
Lecture 3
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).
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.
>> 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
4.5000 4.6000 4.7000 4.8000 4.9000 5.0000 Note: The row vector has 51 elements.