Extract Values Above Main Diagonal of a Matrix in R
Last Updated :
11 Nov, 2022
In this article, we are going to know how to extract the values above the main diagonal of a matrix in the R programming language.
The main diagonal of a matrix is a straight path that connects the entries (or elements) in a matrix whose row and column are the same. The number of elements in the main diagonal is equivalent to either the number of rows or columns in the matrix. In matrix A, for every row i, and every column j of the matrix, the diagonal elements of the matrix are given by the following :
Aij where i=j
Example:
Input Matrix : [[3, 1, 9],
[8, 2, 3],
[7, 11, 4]]
Output : 1, 9, 3
Explanation : In the above matrix the main diagonal is [3, 2, 4]
and we have to extract values above main diagonal elements.
so, the answer will be 1, 9, 3
Creating simple matrix
The matrix can be created using the matrix() method which is used to arrange the specified data elements into the specified number of rows and columns. The method has the following syntax :
Syntax : matrix (data, nrow = rows, ncol = cols)
Parameters :
- data - The data to be arranged into the matrix.
- rows - The number of rows in matrix.
- cols - The number of columns in matrix.
R
# declaring the number of rows
nrow <- 4
# declaring the number of columns
ncol <- 4
# creating matrix
mat <- matrix(1:16, nrow = nrow,
ncol = ncol)
# printing the matrix
print ("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
Extracting the above values of the main diagonal in a matrix
A nested for loop can be used to iterate over rows and columns of the matrix. An outer loop is used to iterate over the row elements and an inner loop is used to iterate over the column elements. And if the condition is used to check if the column number is greater than the row number. The element is then extracted from the matrix.
R
# declaring the number of rows
nrow <- 4
# declaring the number of columns
ncol <- 4
# creating matrix
mat <- matrix(1:16, nrow = nrow,
ncol = ncol)
# printing the matrix
print ("Matrix : ")
print(mat)
# getting the elements above
# the main diagonal elements
# looping over rows
for(row in 1:nrow(mat))
{
# looping over columns
for(col in 1:ncol(mat))
{
# if column number is greater than row
if(col > row)
{
# printing the element of the matrix
print(mat[row,col])
}
}
}
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
A
[1] 5
[1] 9
[1] 13
[1] 10
[1] 14
[1] 15
Similar Reads
Convert a vector into a diagonal matrix in R In this article, we will examine various methods to convert a vector into a diagonal matrix by using R Programming Language. What is a diagonal matrix?A diagonal matrix is a special type of square matrix where all elements, except those on the main diagonal (running from the top-left to the bottom-r
3 min read
Generate a block-diagonal matrix using R R language is a powerful and open-source programming language, is widely used for statistical software and data analysis. One of the many functionalities that R offers is the creation and manipulation of block diagonal matrices, a valuable tool in various mathematical and statistical applications. W
4 min read
Min, Max and Mean of Off-Diagonal Elements in a Matrix in R A matrix is a combination of elements stacked together in either row or column format. A table-like structure formed of similar data type elements is known as a matrix. A matrix has two diagonals, one of which is known as the main diagonal. The main diagonal elements are characterized by the proper
3 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
How to find the Diagonal of a Matrix? British Mathematician Arthur Cayley was the first person to develop the algebraic aspect of the matrix. After that, Psychiat Heisenberg used matrices as a tool to explain his famous Quantum principle. The study of matrices originated while solving different types of simple and complex linear problem
6 min read
Replace the Diagonal of a Matrix using R In this article, we will learn what a is matrix and various methods to replace the diagonal of a matrix in the R Programming Language. What is a matrix?A matrix is a two-dimensional data set, a collection of rows and columns. Inside the matrix, rows are arranged horizontally, and columns are arrange
5 min read
Construct a Diagonal Matrix in R Programming - diag() Function diag() function in R Language is used to construct a diagonal matrix. Syntax: diag(x, nrow, ncol)Parameters: x: value present as the diagonal elements. nrow, ncol: number of rows and columns in which elements are represented.  Example 1:  Python3 # R program to illustrate # diag function # Callin
1 min read
Extract unique columns from a matrix using R A matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. In R programming Language, matrices are two-dimensional, homogeneous data structures. These are some examples of matrice
5 min read
Convert a matrix into a lower triangular matrix using R In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language. What is a matrix?A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are ve
5 min read