This example illustrates a few special types of matrices.
Identity Matrices
An identity matrix is a square matrix where all elements along its diagonal are set to one and the remaining elements are set to zeros.
Use the identity function to generate a 3x3 identity matrix.
Diagonal Matrices
A diagonal matrix is a square matrix where all elements along its diagonal are set to any value and the remaining elements are set to zeros:
1. Define three input vectors:
2. Use the diag function to find the diagonal matrices formed from each vector:
The size of the generated square matrix is always nxn, where n is the length of the input vector.
3. Write a program for forming an upper triangular matrix:
4. Use the program to generate the upper triangular matrix for each vector:
The vector elements are placed in the output matrix starting from the diagonal element (0,0) and continuing to the right until row 0 is full, then from the diagonal element (1,1) until row 1 is full, and so on until the last element of the vector is placed. The size of the output square matrix is large enough to accommodate all elements of the vector.
5. Write a program for forming a lower triangular matrix:
6. Use the program to generate lower triangular matrix for each vector:
The vector elements are placed in the output matrix starting from the diagonal element (0,0), then continuing down starting from element (1,0) until row 1 up to and including diagonal element (1,1) is full, then from element (2,0) until row 2 up to and including diagonal element (2,2) is full, and so on until the last element of the vector is placed. The size of the output square matrix is large enough to accommodate all elements of the vector.