A matrix is a two-dimensional array of numbers.
In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers and using semicolons to mark the end of each row.
For example, let us create a 4-by-5 matrix a −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
MATLAB will execute the above statement and return the following result −
a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
Referencing the Elements of a Matrix
To reference an element in the mth row and nth column, of a matrix mx, we write −
mx(m, n);
For example, to refer to the element in the 2nd row and 5th column, of the matrix a, as created in the last section, we type −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(2,5)
MATLAB will execute the above statement and return the following result −
ans = 6
To reference all the elements in the mth column we type A(:,m).
Let us create a column vector v, from the elements of the 4th row of the matrix a −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; v = a(:,4)
MATLAB will execute the above statement and return the following result −
v = 4 5 6 7
You can also select the elements in the mth through nth columns, for this we write −
a(:,m:n)
Let us create a smaller matrix taking the elements from the second and third columns −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(:, 2:3)
MATLAB will execute the above statement and return the following result −
ans = 2 3 3 4 4 5 5 6
In the same way, you can create a sub-matrix taking a sub-part of a matrix.
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(:, 2:3)
MATLAB will execute the above statement and return the following result −
ans = 2 3 3 4 4 5 5 6
In the same way, you can create a sub-matrix taking a sub-part of a matrix.
For example, let us create a sub-matrix sa taking the inner subpart of a −
3 4 5 4 5 6
To do this, write −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; sa = a(2:3,2:4)
MATLAB will execute the above statement and return the following result −
sa = 3 4 5 4 5 6
Deleting a Row or a Column in a Matrix
You can delete an entire row or column of a matrix by assigning an empty set of square braces [] to that row or column. Basically, [] denotes an empty array.
For example, let us delete the fourth row of a −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a( 4 , : ) = []
MATLAB will execute the above statement and return the following result −
a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7
Next, let us delete the fifth column of a −
Live Demoa = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(: , 5)=[]
MATLAB will execute the above statement and return the following result −
a = 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7
Example
In this example, let us create a 3-by-3 matrix m, then we will copy the second and third rows of this matrix twice to create a 4-by-3 matrix.
Create a script file with the following code −
Live Demoa = [ 1 2 3 ; 4 5 6; 7 8 9]; new_mat = a([2,3,2,3],:)
When you run the file, it displays the following result −
new_mat = 4 5 6 7 8 9 4 5 6 7 8 9
Matrix Operations
In this section, let us discuss the following basic and commonly used matrix operations −
You can add or subtract matrices. Both the operand matrices must have the same number of rows and columns.
Example
Create a script file with the following code −
a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = a + b d = a - b
When you run the file, it displays the following result −
c = 8 7 9 6 5 14 12 15 10 d = -6 -3 -3 2 5 -2 2 1 8
You can divide two matrices using left (\) or right (/) division operators. Both the operand matrices must have the same number of rows and columns.
Example
Create a script file with the following code −
a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = a / b d = a \ b
When you run the file, it displays the following result −
c = -0.52542 0.68644 0.66102 -0.42373 0.94068 1.01695 -0.32203 1.19492 1.37288 d = -3.27778 -1.05556 -4.86111 -0.11111 0.11111 -0.27778 3.05556 1.27778 4.30556
When you add, subtract, multiply or divide a matrix by a number, this is called the scalar operation.
Scalar operations produce a new matrix with same number of rows and columns with each element of the original matrix added to, subtracted from, multiplied by or divided by the number.
Example
Create a script file with the following code −
a = [ 10 12 23 ; 14 8 6; 27 8 9]; b = 2; c = a + b d = a - b e = a * b f = a / b
When you run the file, it displays the following result −
c = 12 14 25 16 10 8 29 10 11 d = 8 10 21 12 6 4 25 6 7 e = 20 24 46 28 16 12 54 16 18 f = 5.0000 6.0000 11.5000 7.0000 4.0000 3.0000 13.5000 4.0000 4.5000
The transpose operation switches the rows and columns in a matrix. It is represented by a single quote(').
Example
Create a script file with the following code −
a = [ 10 12 23 ; 14 8 6; 27 8 9] b = a'
When you run the file, it displays the following result −
a = 10 12 23 14 8 6 27 8 9 b = 10 14 27 12 8 8 23 6 9
You can concatenate two matrices to create a larger matrix. The pair of square brackets '[]' is the concatenation operator.
MATLAB allows two types of concatenations −
- Horizontal concatenation
- Vertical concatenation
When you concatenate two matrices by separating those using commas, they are just appended horizontally. It is called horizontal concatenation.
Alternatively, if you concatenate two matrices by separating those using semicolons, they are appended vertically. It is called vertical concatenation.
Example
Create a script file with the following code −
a = [ 10 12 23 ; 14 8 6; 27 8 9] b = [ 12 31 45 ; 8 0 -9; 45 2 11] c = [a, b] d = [a; b]
When you run the file, it displays the following result −
a = 10 12 23 14 8 6 27 8 9 b = 12 31 45 8 0 -9 45 2 11 c = 10 12 23 12 31 45 14 8 6 8 0 -9 27 8 9 45 2 11 d = 10 12 23 14 8 6 27 8 9 12 31 45 8 0 -9 45 2 11
Consider two matrices A and B. If A is an m x n matrix and B is an n x p matrix, they could be multiplied together to produce an m x n matrix C. Matrix multiplication is possible only if the number of columns n in A is equal to the number of rows n in B.
In matrix multiplication, the elements of the rows in the first matrix are multiplied with corresponding columns in the second matrix.
Each element in the (i, j)th position, in the resulting matrix C, is the summation of the products of elements in ith row of first matrix with the corresponding element in the jth column of the second matrix.
Matrix multiplication in MATLAB is performed by using the * operator.
Example
Create a script file with the following code −
a = [ 1 2 3; 2 3 4; 1 2 5] b = [ 2 1 3 ; 5 0 -2; 2 3 -1] prod = a * b
When you run the file, it displays the following result −
a = 1 2 3 2 3 4 1 2 5 b = 2 1 3 5 0 -2 2 3 -1 prod = 18 10 -4 27 14 -4 22 16 -6
Determinant of a matrix is calculated using the det function of MATLAB. Determinant of a matrix A is given by det(A).
Example
Create a script file with the following code −
a = [ 1 2 3; 2 3 4; 1 2 5] det(a)
When you run the file, it displays the following result −
a = 1 2 3 2 3 4 1 2 5 ans = -2
The inverse of a matrix A is denoted by A−1 such that the following relationship holds −
AA−1 = A−1A = 1
The inverse of a matrix does not always exist. If the determinant of the matrix is zero, then the inverse does not exist and the matrix is singular.
Inverse of a matrix in MATLAB is calculated using the inv function. Inverse of a matrix A is given by inv(A).
Example
Create a script file and type the following code −
a = [ 1 2 3; 2 3 4; 1 2 5] inv(a)
When you run the file, it displays the following result −
a = 1 2 3 2 3 4 1 2 5 ans = -3.5000 2.0000 0.5000 3.0000 -1.0000 -1.0000 -0.5000 0 0.5000
No comments:
Post a Comment