How to Create Tridiagonal Matrix in R
Last Updated :
02 Aug, 2022
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
mat<- matrix (,nrow=4,ncol=4)
print ( "Empty matrix" )
print (mat)
for (row in 1: nrow (mat)) {
for (col in 1: ncol (mat)) {
if ( abs (row-col) > 1){
mat[row,col] = 0
}
else {
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
rows <- 4
mat <- matrix (1:16,
nrow = rows
)
print ( "Matrix" )
print (mat)
mat[ abs ( row (mat) - col (mat)) > 1] = 0
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
Similar Reads
How to create a matrix in R
In this article, we will discuss What is a matrix and various methods to create a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data set that collects rows and columns. The matrix stores the data in rows and columns format. It is possible to access the data i
3 min read
Create Matrix from Vectors in R
In this article, we will discuss How to convert a vector to a matrix in R Programming Language. A vector is a basic object that consists of homogeneous elements. The data type of vector can be integer, double, character, logical, complex, or raw. A vector can be created by using the c() function. Sy
5 min read
How to Create an Exchange Matrix in R
In this article, we will discuss how to create an exchange matrix in R Programming Language. The exchange matrix is a square matrix with ones on the anti-diagonal and zeros on all other elements. We can say that the exchange matrix is a combination of the identity matrix and the anti-diagonal matrix
1 min read
How to Create a Distance Matrix in R?
A distance matrix is a matrix that contains the distance between each pair of elements in a dataset. In R Programming Language, there are several functions available for creating a distance matrix such as dist(), daisy(), and vegdist() from the stats, cluster, and vegan packages respectively. Distan
8 min read
How to Create a Covariance Matrix in R?
In this article, we will discuss how to create a Covariance Matrix in the R Programming Language. Covariance is the statistical measure that depicts the relationship between a pair of random variables that shows how the change in one variable causes changes in another variable. It is a measure of th
2 min read
How to Create Anti-Diagonal Matrix in R
In this article, we will discuss how to create an anti-diagonal matrix with its working example in the R programming language. Anti-Diagonal Matrix: The anti-diagonal matrix is a square matrix where all entries are zero except for those on the anti-diagonal. That is to say, the diagonal goes from th
2 min read
Create matrix of zeros in R
R programming language offers us a variety of ways to create a matrix and fill it in such a way that all the element values are equivalent to 0. Let's see those ways - Using matrix() method The in-built matrix() method in R can be used to create a matrix with a given set of values, that is, n x m di
4 min read
How to Create Tables in R?
In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: I
2 min read
How to Create a Two Way Table in R?
In this article, we will create a two-way table in R programming language. A two-way table is used to display frequency for two categorical variables. The rows represent the categorical features and the column represents frequency. We can create two-way table using as.table() method. as.table() func
2 min read
How to Create Pivot Tables in R?
In this article, we will discuss how to create the pivot table in the R Programming Language. The Pivot table is one of Microsoft Excel's most powerful features that let us extract the significance from a large and detailed data set. A Pivot Table often shows some statistical value about the dataset
2 min read