IS1301: Engineering Mathematics I
Department of Interdisciplinary Studies
Faculty of Engineering
University of Sri Jayewardenepura
Matrix Computations
3 Arrays in MATLAB
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. In science and engineering, one-dimensional arrays
frequently represent vectors, and two-dimensional arrays often represent matrices.
Creating a Two-Dimensional Array (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 [ ].
Variable_name = [1st row elements; 2nd row elements; 3rd row elements; ... ; last row elements]
Example:
1 2
If the matrix A = [4 5
7 8
3
6 ], then A can be created typing the following syntax,
10
A = [1 2 3; 4 5 6; 7 8 10]
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.
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 M, M(r,c) refers to the
element in row r and column c.
For example if the matrix is, M = [3 11 6 5; 4 7 10 2; 14 9 0 8]
Then,
𝑀(3,1) = 14
𝑀(2,4) − 𝑀(1,2) = −9
Department of Interdisciplinary Studies
Faculty of Engineering, University of Sri Jayewardenepura
IS1301: Engineering Mathematics I
1
3.2.1 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:
V(:)
V(m:n)
Refers to all the elements of the vector V (either a row or a column vector).
Refers to elements m through n of the vector V.
Example:
For a matrix:
A(:,n)
A(n,:)
A(:,m:n)
A(m:n,:)
A(m:n,p:q)
Refers to the elements in all the rows of column n of the matrix A.
Refers to the elements in all the columns of row n of the matrix A.
Refers to the elements in all the rows between columns m and n of the matrix A.
Refers to the elements in all the columns between rows m and n of the matrix A.
Refers to the elements in rows m through n and columns p through q of the
matrix A.
Example:
Special Matrix Commands
We can also use the following commands to create special matrices.
Table 1: Special matrix commands
Command
Description
eye(n)
Creates an n x n identity matrix.
eye(size(A))
Creates an identity matrix the same size as the matrix A.
ones(n)
Creates an n x n matrix of ones.
zeros(n)
Creates an n x n matrix of zeros.
rand(m,n)
Creates an m x n array of uniformly distributed random numbers.
Adding Elements to Existing Variables
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.
2
Department of Interdisciplinary Studies
Faculty of Engineering, University of Sri Jayewardenepura
IS1301: Engineering Mathematics I
Example:
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
to have a smaller size.
Example:
Matrix Operations in MATLAB
3.6.1 Addition and Subtraction
When two arrays are involved the sum, or the difference, of the arrays is obtained by adding,
or subtracting, their corresponding elements.
Example:
Note:
When a scalar (number) is added to (or subtracted from) an array, the scalar is added to (or
subtracted from) all the elements of the array.
3.6.2 Matrix Multiplication
If A and B are two matrices, the operation A*B can be carried out only if the number of
columns in matrix A is equal to the number of rows in matrix B.
Note:
1. The multiplication of matrices is not commutative. That is if A and B are both n x n , then,
A*B≠B*A
2. The power operation (^) can be executed only with a square matrix.
A^2 (A has to be a square matrix)
3. The power (-1) denotes the inverse of a matrix.
Example:
Department of Interdisciplinary Studies
Faculty of Engineering, University of Sri Jayewardenepura
IS1301: Engineering Mathematics I
3
3.6.3 Matrix Division
MATLAB has two types of array division, left division and right division.
Left division, \
Left division is used to solve the matrix equations. Consider the equation AX = B. X and B are
column vectors. The solution of AX =B is,
𝑋 = 𝐴–1 𝐵
This can be solved by using the left division as follows,
𝑋 = 𝐴\𝐵
Right division, /
The right division is used to solve the matrix equations as well. Consider the equation XC =D,
X and D are row vectors. The solution of 𝑋𝐶 = 𝐷 is,
𝑋 = 𝐷𝐶 –1
This can be solved by using the right division as follows,
𝑋 = 𝐷/𝐶
3.6.4 Element-wise Operations
In the above sections it was shown that when the regular symbols for multiplication and
division (* and /) are used with arrays, the mathematical operations follow the rules of linear
algebra. There are, however, many situations that require element-by-element operations.
These operations are carried out on each of the elements of the array (or arrays). Addition
and subtraction are by definition already element-by-element operations since when two
arrays are added (or subtracted) the operation is executed with the elements that are in the
same position in the arrays. Element-by-element operations can be done only with arrays of
the same size.
Element-by-element multiplication, division, or exponentiation of two vectors or matrices is
entered in MATLAB by typing a period in front of the arithmetic operator.
Table 2: Element-by-element syntax
Operation
Algebraic Form
MATLAB Syntax
Multiplication
Division
Exponentiation
axb
a÷b
ab
a.*b
a./b
a.^b
Example:
4
Department of Interdisciplinary Studies
Faculty of Engineering, University of Sri Jayewardenepura
IS1301: Engineering Mathematics I
Built-In Functions for Analyzing Arrays
MATLAB has many built-in functions for analyzing arrays. Table 3 lists some of these functions.
Table 3: Built-in array functions
Function
transpose(A) or A’
numel(A)
length(A)
size(A)
reshape(A,m,n)
diag(A)
det(A)
inv(A)
rank(A)
mean(A)
sum(A)
Description
Returns the transpose of A
Returns the number of elements in array A
Returns the number of elements in the vector A
Returns a row vector [m,n],where m and n are the size m x n of the array A
Creates m by n matrix from matrix A. The elements are taken column after
column. Matrix A should have m by n elements
When A is a matrix, creates a vector from the diagonal elements of A
Returns the determinant of a square matrix A
Returns the inverse of a square matrix A
Returns the rank of matrix A
If A is a matrix, the answer is a row vector containing the mean of each
column of A
If A is a matrix, the answer is a row vector containing the sums of each
column of A
Solving System of Linear Equations
Consider the following equations
4𝑥– 2𝑦 + 6𝑧 = 8
2𝑥 + 8𝑦 + 2𝑧 = 4
6𝑥 + 10𝑦 + 3𝑧 = 0
Using the rules of linear algebra, the above system of equations can be written in the matrix
form 𝐴𝑋 = 𝐵 or in the form 𝑋𝐶 = 𝐷.
A
X
B
X
C
D
3.8.1 Solving the form AX = B
A = [4 -2 6; 2 8 2; 6 10 3]; B = [8; 4; 0];
Solving the system using left division
𝑋 = 𝐴\𝐵
Solving using the inverse
𝑋 = 𝑖𝑛𝑣(𝐴) ∗ 𝐵
3.8.2 Solving the form XC = D
C = [4 2 6; -2 8 10; 6 2 3]; D = [8 4 0];
Solving the system using right division
𝑋 = 𝐷/𝐶
Department of Interdisciplinary Studies
Faculty of Engineering, University of Sri Jayewardenepura
IS1301: Engineering Mathematics I
5