Arrays, Vectors and Matrices
An array is a collection of values that are represented by a single variable.
• A one-dimensional array is called a vector
• Multi-dimensional arrays are called matrices
• In MATLAB, any scalar value is considered to be a 1 x 1 matrix.
Scalars: A scalar can be created as follows:
>> x = 25
Arrays: In the command mode, square brackets are used to create arrays. For
example, the row vector a = (1, 2, 3, 4, 5) can be created as follows
>> a = [1 2 3 4 5] or
>> a = [1, 2, 3, 4, 5]
1
Arrays, Vectors and Matrices
A column vector can be created as follows
>> b = [1; 2; 3; 4; 5] or
>> b = [1, 2, 3, 4, 5]' or
>> b = [1
2
3
4
5]
Matrices: A matrix of values can be created as follows:
>> A = [1,2,3;4,5,6;7,8,9]
This creates a 3 by 3 matrix of values 1 2 3
4 5 6
7 8 9 2
Accessing Elements of Arrays/Matrices
The process of accessing the element of an array is known as array subscripting or
indexing.
Accessing the Elements of a Vector: The syntax for accessing the element of a
vector is
b(m)
where b is the vector and m is the element’s position or number.
Examples: Consider the vector b = [3, 2, 5, 4, 7, 8, 6]
The first, second and third elements of the vector b are accessed as
>> b(1) >> b(2) >> b(3) 3
Accessing Elements of Arrays/Matrices
Given the vector b = [3, 2, 5, 4, 7, 8, 6], find the result of the following operations:
(a) b(1) + b(3)
(b) b(2)*3 + 2*b(4)
>> b = [3, 2, 5, 4, 7, 8, 6]
b=
3 2 5 4 7 8 6
>> b(1) + b(3)
ans =
8
>> b(2)*3 + 2*b(4)
ans =
14 4
Accessing Elements of Arrays/Matrices
Accessing the Elements of a Matrix: The syntax for accessing the element of a
matrix is
A(m,n)
where A is the matrix, m is the element’s row number, and n is the column number.
Examples: (1) Consider the matrix A = [6,2,5,3;7,4,8,9;1,10,11,12]
6 2 5 3 Columns (n) Therefore, the elements 6, 5 and 4
7 4 8 9 1 2 3 4 can be accessed respectively as
1 6 4 8 >> A(1, 1)
1 6 2 5 3
Rows (m)
>> A(1, 3)
2 7 4 8 9 >> A(2, 2)
3 1 10 11 12
5
Accessing Elements of Arrays/Matrices
6 2 5 3
A= 7 4 8 9 A(1, 1) means element of A at row 1 and column 1
1 6 4 8 A(1, 3) means element of A at row 1 and column 3
A(2, 2) means element of A at row 2 and column 2
Columns (n)
1 2 3 4 Self-Assessment: Write down the results of the
1 6 2 5 3 following indexing commands for matrix A.
Rows (m)
>> A(2, 1)
2 7 4 8 9 >> A(3, 2)
3 1 10 11 12 >> A(2, 3)
>> A(1, end)
>> A(3, end-1)
>> A(end, end)
6
Matrix Generating Commands
There are several built-in commands or functions for automatically creating arrays.
For example, (1) the command ones(m,n) creates an m by n matrix filled with
ones
>> ones(3,3)
1 1 1
1 1 1
1 1 1
(2) The zeros command is used to create an m by n matrix filled with zeros.
>> zeros(3,2)
0 0
0 0
0 0 7
Arrays, Vectors and Matrices
Self-assessment Exercise: Use MATLAB’s help command to search for help or
usage information on the following matrix generation commands:
(a) eye
(b) rand
The Colon Operator
The colon operator is used for creating and manipulating arrays. For example, the
command line operation
>> x = 1:5
means generate the numbers from 1 to 5 at an increment or interval of 1
>> x = 1:5
x =
1 2 3 4 5
8
The Colon Operator (:)
The general syntax for the increment operator is a : b : c
which means generate numbers starting from a and ending at c with an
increment of b. When the incremental value is excluded a default value of
b = 1 is used.
>> x = 1:0.5:3
x =
1.0000 1.5000 2.0000 2.5000 3.0000
Negative increments can also be used.
>> x = 10:-1:6
x =
10 9 8 7 6
9
Using the Colon Operator for Range Indexing
The column operator can be used to access or reference a range of elements in an
array. For example, given the vector
b = [3, 2, 5, 4, 7, 8, 6],
to access the range of elements from position 1 to 4, the syntax will be
>> b(1:4)
ans =
3 2 5 4
Similarly, to access elements from position 3 to 6, the syntax and output will be.
>> b(3:6)
ans =
5 4 7 8
10
Using the Colon Operator for Range Indexing
Self-Assessment: Given the vector b = [3, 2, 5, 4, 7, 8, 6], determine the output of
the following statements
>> b(2:5)
>> b(3:end)
>> b(5:-1:2)
>> b(5:-1:2)*2
>> b(1:4) + b(4:7)
>> b(3:end-1).^2
11
Range Indexing of Matrices
The syntax for range indexing a matrix is
A(m1:m2,n1:n2)
which stands for the range of elements in rows m1 to m2 and columns n1 to n2.
Examples: Given the matrix A = [6,2,5,3;7,4,8,9;1,10,11,12],
6 2 5 3
A= 7 4 8 9
1 6 4 8
we can access the range of elements in rows 1 to 2 and columns 1 to 3 as
>> A(1:2,1:3)
ans = 6 2 5
7 4 8
12
Range Indexing of Matrices
6 2 5 3
A= 7 4 8 9
1 6 4 8
>> A(2:3,2:end)
>> A(:,1) >> A(:,:)
ans = 4 8 9
ans = 6 ans = 6 2 5 3
6 4 8
7 7 4 8 9
>> A(1,1:end) 1 1 6 4 8
ans = 6 2 5 3
>> A(1,:) >> A(:,3:4)
ans = 6 2 5 3 ans = 5 3
8 9
>> A(1:2,:) 4 8
ans = 6 2 5 3
7 4 8 9 13
Further Array/Matrix Operations
(1) Changing the Elements of an Array
Consider the matrix given by A = [1,2,3;4,5,6;7,8,9],
>> A = [1,2,3;4,5,6;7,8,9] <enter>
A =
1 2 3
4 5 6
7 8 9
To set the element A(2, 3) to zero, for example, we use the command
>> A(2,3) = 0 <enter>
A =
1 2 3
4 5 0
7 8 9 14
Further Array/Matrix Operations
A =
1 2 3
4 5 6
7 8 9
To replace the elements in rows 1 to 2, columns 1 and 3 with the 2-by-2 matrix
[12 21;23 32], we use the command
>> A(1:2,[1 3]) = [12 21;23 32] <enter>
A =
12 2 21 >> A = [1,2,3;4,5,6;7,8,9]
23 5 32 >> A(2,:) = []
7 8 9 A = 1 2 3
We can also delete a row or column 7 8 9
of A using the empty vector operator, []. 15
How to Determine the Dimensions of an Array
To determine the dimensions of a matrix, use the size command. The syntax is
as follows:
[m,n] = size(A)
where A is the matrix and output arguments m and n correspond to the number of
rows and columns of the matrix, respectively. E.g., given matrix A = [1 2 3;4
5 6;7 8 9], the dimensions of A are given by
>> [m,n] = size(A) <ent>
m = 3
n = 3
When the size command is called on a vector, it returns with one of the
dimensions equal to 1.
16
How to Determine the Dimensions of an Array
When the size command is called on a vector, it returns with one of the
dimensions equal to 1. E.g.,
>> v = A(2,:); <ent>
>> size(v) <ent>
ans =
1 3
To determine the number of elements in a vector, use the length command. E.g.,
>> length(v)
ans =
3
17
How to Reshape a Matrix
A matrix of dimensions (m x n) can be reshaped to another matrix of dimensions
(p x q) by using the MATLAB reshape command. The syntax is
B = reshape(A,p,q)
where A is the input matrix, p and q are the new dimensions such that mn = pq, and
B is the output matrix.
>> A = [3 4 5;8 7 9] <ent>
A = B =
3 4 5 3 7
8 7 9 8 5
4 9
>> B = reshape(A, 3, 2) <ent>
18