MATLAB Tutorials class 4 : MATLAB command diag, det, trace
MATLAB Tutorials class 4: MATLAB command
diag, det, trace
We discussed random number generation in my previous post (rand, randi,randn function). We're going to see some interesting Matlab commands and their functions in this post. They are diag,det, and trace. First, we going to see about diag
diag
By using the diag command we can create a diagonal matrix and can compute the diagonal of the matrix. Diagonal elements are one in the identity matrix(eye), but we can create our desired diagonal element by using the diag command. This is the major difference between the diagonal matrix (diag) and the identity matrix (eye).
1. How to create a diagonal matrix in MATLAB?
The syntax for creating a diagonal matrix in MATLAB is
diag_creation=diag([1 2 3 4 5])
>> diag_creation=diag([1 2 3 4 5])
diag_creation =
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
>> diag_creation=diag([1 2 3 4 5 4 5]) or
diag_creation=diag([1 2 3 4 5 4 5],0)
diag_creation =
1 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 3 0 0 0 0
0 0 0 4 0 0 0
0 0 0 0 5 0 0
0 0 0 0 0 4 0
0 0 0 0 0 0 5
We can shift the diagonal element to some other location (i.e. super diagonal) as per given below.
>> diag_creation=diag([1 2 3 4 5 4 5],1)
diag_creation =
0 1 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 3 0 0 0 0
0 0 0 0 4 0 0 0
0 0 0 0 0 5 0 0
0 0 0 0 0 0 4 0
0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0
>> diag_creation=diag([1 2 3 4 5 4 5],2)
diag_creation =
0 0 1 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0
0 0 0 0 3 0 0 0 0
0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 5 0 0
0 0 0 0 0 0 0 4 0
0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
>> diag_creation=diag([1 2 3 4 5 4 5],3)
diag_creation =
0 0 0 1 0 0 0 0 0 0
0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 3 0 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 0 5 0 0
0 0 0 0 0 0 0 0 4 0
0 0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
>> diag_creation=diag([1 2 3 4 5 4 5],-1)
diag_creation =
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0
0 0 3 0 0 0 0 0
0 0 0 4 0 0 0 0
0 0 0 0 5 0 0 0
0 0 0 0 0 4 0 0
0 0 0 0 0 0 5 0
>> diag_creation=diag([1 2 3 4 5 4 5],-2)
diag_creation =
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0
0 0 0 0 5 0 0 0 0
0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 5 0 0
>> diag_creation=diag([1 2 3 4 5 4 5],-3)
diag_creation =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0
0 0 0 0 0 4 0 0 0 0
0 0 0 0 0 0 5 0 0 0
2. How to find the diagonal of the matrix in MATLAB?
The same syntax is used to find the diagonal of the matrix. By passing the matrix into the diag command, It will find a diagonal of the matrix.
First, we create a 5 x 5 random matrix using randi
>> rand_matrix_creation=randi(20,5)
rand_matrix_creation =
16 6 6 11 11
17 17 3 3 9
18 9 3 18 2
2 19 18 13 5
8 4 12 8 3
>> diag_creation=diag(rand_matrix_creation)
diag_creation =
16
17
3
13
3
>> diag_creation=diag(rand_matrix_creation,1)
diag_creation =
6
3
18
5
>> diag_creation=diag(rand_matrix_creation,2)
diag_creation =
6
3
2
>> diag_creation=diag(rand_matrix_creation,-1)
diag_creation =
17
9
18
8
>> diag_creation=diag(rand_matrix_creation,-2)
diag_creation =
18
19
12
For example, if suppose we have a matrix like
rand_matrix_creation =
16 6 6 11 11
17 17 3 3 9
18 9 3 18 2
2 19 18 13 5
8 4 12 8 3
If the diagonal elements should be according to the matrix and 0 should be the remainder we can do as given below
>> diag_creation=diag(diag(rand_matrix_creation))
diag_creation =
16 0 0 0 0
0 17 0 0 0
0 0 3 0 0
0 0 0 13 0
0 0 0 0 3
trace
Next, we're going to see MATLAB's trace command.
How to compute the sum of the diagonal of the matrix in MATLAB?
Trace command is used to compute the sum of the diagonal matrix in MATLAB. Syntax is
tracr_of_matrix=trace(matrix)
>> rand_matrix_creation=randi(20,5)
rand_matrix_creation =
1 13 4 16 9
1 10 14 2 9
4 11 4 19 7
13 6 8 16 11
15 15 13 10 11
>> trace_of_value=trace(rand_matrix_creation)
trace_of_value =
42
det
How to compute the determinant of the matrix in MATLAB?
det command is used to compute the determinant of the matrix in MATLAB. Syntax is
det_of_matrix=det(matrix)
>> rand_matrix_creation=randi(5,3)
rand_matrix_creation =
2 3 3
1 2 1
2 5 5
>> det_of_matrix=det(rand_matrix_creation)
det_of_matrix =
4
Reference:
Thanks to MATLAB Documentation
We have discussed the diag,det, trace inbuild command in Matlab so far. We will see the next topic in my next post.
Thank you so much😇
Please subscribe😊.
Share with your friends and please provide your feedback about this post🙏🙏.
Comments
Post a Comment