# How to Create Tridiagonal Matrix in R

A matrix is a combination of cells arranged together in a tabular format. A matrix contains elements belonging to the same data type. There are m x n elements in a matrix where m is the number of rows and n is the number of columns respectively.

**A tridiagonal matrix is a matrix which has the following attributes : **

- All the entries on the main diagonal should be non-zero.
- All the elements on the first lower diagonal should be non-zero.
- All the elements on the first upper diagonal should be non-zero.
- All the remaining elements should be zero.

The creation of tridiagonal matrix is based on the concept that the difference in the row and column number for the non-zero elements is less than equal to 1.

**Creating a tridiagonal matrix in R Programming Language**

**Method 1: Using iteration**

A for loop can be used to iterate over the elements of the matrix. An outer for loop is used to iterate over the rows of the matrix and an inner for loop is used to iterate over the columns. Therefore, two loops are required. The row and column number is then validated using the if condition. The absolute difference of the row and column number is compared. If the value is greater than the constant 1, then the element of the matrix is re-written with the value 0, else 1 or any other number.

## R

`# creating a matrix` `mat<-` `matrix` `(,nrow=4,ncol=4)` ` ` `print` `(` `"Empty matrix"` `)` `print` `(mat)` ` ` `# looping over rows` `for ` `(row ` `in` `1:` `nrow` `(mat)) {` ` ` ` ` `# looping over columns` ` ` `for ` `(col ` `in` `1:` `ncol` `(mat)) {` ` ` ` ` `# checking if absolute difference` ` ` `# between the row and column is` ` ` `# greater than 1` ` ` `if` `(` `abs` `(row-col) > 1){` ` ` ` ` `# assigning the value of 0 ` ` ` `mat[row,col] = 0` ` ` `}` ` ` `else` `{` ` ` ` ` `# assigning value 1 if not satisfactory` ` ` `mat[row,col] = 1` ` ` `}` ` ` `}` `}` `print` `(` `"Tridiagonal Matrix"` `)` `print` `(mat)` |

**Output**

[1] "Empty matrix" [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA NA NA NA [3,] NA NA NA NA [4,] NA NA NA NA [1] "Tridiagonal Matrix" [,1] [,2] [,3] [,4] [1,] 1 1 0 0 [2,] 1 1 1 0 [3,] 0 1 1 1 [4,] 0 0 1 1

**Explanation : **

Initially, an empty matrix with the NA values is created by specifying the dimensions. The values are then re-written based on the formula stated above.

**Method 2: Using addressing methods **

A tridiagonal matrix can be created using simple addressing methods. The matrix can be created using the simple absolute difference formula. If the difference between the respective row and column number of the matrix is more than 1, then this element value is equivalent to 0. Otherwise, the element value is non-zero.

For any row and column number of the matrix mat, the following formula can be checked for the matrix :

abs(row(mat) - col(mat)) > 1

All the values satisfying the above formula, are equivalent to 0.

## R

`# specifying the number of rows` `rows <- 4` ` ` `# creating a matrix` `mat <- ` `matrix` `(1:16,` ` ` `nrow = rows` ` ` `)` `# printing matrix` `print` `(` `"Matrix"` `)` `print` `(mat)` ` ` `# checking the absolute difference` `# is greater than 1 or not ` `# setting that value equal to 0` `mat[` `abs` `(` `row` `(mat) - ` `col` `(mat)) > 1] = 0` ` ` `# print tridiagonal matrix` `print` `(` `"Tridiagonal Matrix"` `)` `print` `(mat)` |

**Output**

[1] "Matrix" [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 [1] "Tridiagonal Matrix" [,1] [,2] [,3] [,4] [1,] 1 5 0 0 [2,] 2 6 10 0 [3,] 0 7 11 15 [4,] 0 0 12 16