
Identity Matrices in Matlab
In this lesson, I will explain how to generate an identity matrix in Matlab.
So, what is an identity matrix? An identity matrix ( unit matrix ) is a square matrix with elements equal to 1 on the main diagonal and 0 elsewhere. For example, here's a 3x3 identity matrix: $$ I = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} $$
To generate an identity matrix in Matlab, you can use the eye(n) function.
eye(n)
where 'n' is the number of rows/columns of the square matrix.
Let's take a look at a practical example.
To create a 2x2 identity matrix, simply type eye(2)
>> eye(2)
This command will create a 2x2 identity matrix.
ans =
1 0
0 1
Similarly, to create a 3x3 identity matrix, type eye(3)
>> eye(3)
The result will be a 3x3 identity matrix.
ans =
1 0 0
0 1 0
0 0 1
You can define an identity matrix of any size using this approach.
In addition, the eye(m,n) function with two parameters allows you to create a rectangular matrix that includes an identity matrix as a square submatrix.
eye(m,n)
where 'm' is the number of rows and 'n' is the number of columns of the rectangular matrix.
For example, to create a 2x3 matrix with an identity submatrix, type eye(2,3)
>> eye(2,3)
Matlab will generate a rectangular matrix with an identity matrix inside.
ans =
1 0 0
0 1 0