MATLAB Programming/Vector and Matrices/Special Matrices
Special Matrices
editMatrix of ones
editWe can create matrix consists using the functions ones with m numbers of rows and n numbers of columns
>> ones(3,3)
ans =
1 1 1
1 1 1
1 1 1
Matrix of zeroes
editWe can create matrix consists using the functions zeros with m numbers of rows and n numbers of columns
>> zeros(3,3)
ans =
0 0 0
0 0 0
0 0 0
Identity Matrices
editAn identity matrix is a square matrix in which each of the elements of its diagonal is a 1 and each of the other elements is a 0.
An identity matrix is used for following purposes:
(a) To verify whether any two given matrices are inverses of each other.
A and B in examples below are inverse to one another
>> A=[3,-2;-1,1]
A =
3 -2
-1 1
>> B=[1,2;1,3]
B =
1 2
1 3
>> A*B
ans =
1 0
0 1
(b) To find the inverse of a matrix
Note 1: Not every inverse matrix can use have identity matrix
Note 2: Command "eye(n)" can be used to create identity matrix in a flash, n is the size of matrix
>> A=[3,2;4,3]
A =
3 2
4 3
>> eye(2)
ans =
1 0
0 1
>> eye(2)/A
ans =
3 -2
-4 3
(c) To find the eigenvalues and eigenvectors.
Eigenvalue is defined as a scalar associated with a given linear transformation of a vector space and having the property that there is some non-zero vector which when multiplied by the scalar is equal to the vector obtained by letting the transformation operate on the vector.
Let say we have a matrix A as followed:
To find lambda, we need to know the equation for finding eigenvalue with following formula :
But MATLAB provided a simple way to find eigenvalue using command "eig"
>> A=[1,4;3,2]
A =
1 4
3 2
>> lambda = eig(A)
lambda =
-2
5
Magic Square Matrices
editA magic square is basically where the sum of the elements in each column and the sum of the elements in each row are the same and there are no repeating numbers. We can create a magic square use this command M=magic(n). The order n must be a scalar greater than or equal to 3 in order to create a valid magic square.
For more info, can refer to this well written Wikibooks on this topic: Puzzles/Luoshu Squares
For example , we can create a magic square with matrices 5 by 5
>> % creating 5X5 matrix magic square
>> c = magic(5)
c =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9