Vectors, Matrices, and Tables > Working With Arrays > Example: Special Types of Matrices
Example: Special Types of Matrices
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:
 u 3 2 1 v 5 4 3 2 1 w 11 10 9 8 7 6 5 4 3 2 1
2. Use the diag function to find the diagonal matrices formed from each vector:
 diag u diag v diag w
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:
 upptriang u upptriang v upptriang w
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:
 lowtriang u lowtriang v lowtriang w
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.