Min, Max and Mean of Off-Diagonal Elements in a Matrix in R
Last Updated :
02 Aug, 2022
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 property of having the ith index equivalent to the jth index, where i and j are the row and column numbers of the matrix respectively.
How to get Min, Max and Mean of Off-Diagonal Elements in a Matrix in R
The off-diagonal elements are represented by the fact the row number is not equal to the column number of the matrix. This can be validated using the not equal to logical operator. This is then fetched using the matrix addressing method then.
The row() method can be used to fetch the row of the matrix and the col() method is used to fetch the column.
mat[row(mat)!=col(mat)]
Example 1: Min of Off-Diagonal Elements
The min() method can be used to fetch the minimum of the supplied elements. It is a method available in base R. The minimum element is returned as the output.
min(list-of-elements)
R
# create matrix in R
mat = matrix(
# sequence of elements
c(1:16),
# No of rows
nrow = 4,
byrow = TRUE
)
print("Matrix")
print(mat)
# calculating the off-diagonal elements
ele = mat[row(mat)!=col(mat)]
# calculating the minimum of these \
# elements
print("Min of off diagonal elements")
min(ele)
Output
[1] "Matrix"
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[1] "Min of off diagonal elements"
[1] 2
Example 2: Max of Off-Diagonal Elements
The max() method can be used to fetch the maximum of the supplied elements. It is a method available in base R. The maximum element is returned as the output.
max(list-of-elements)
R
# create matrix in R
mat = matrix(
# sequence of elements
c(1:16),
# No of rows
nrow = 4,
byrow = TRUE
)
print("Matrix")
print(mat)
# calculating the off-diagonal elements
ele = mat[row(mat)!=col(mat)]
# calculating the minimum of these
# elements
print("Max of off diagonal elements")
max(ele)
Output
[1] "Matrix"
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[1] "Max of off diagonal elements"
[1]15
Example 3: Mean of Off-Diagonal Elements
Mean is the sum of all elements in a list divided by the number of such elements. The mean() method in base R is used to calculate the mean of elements specified in the argument.
mean(list-of-elements)
R
# create matrix in R
mat = matrix(
# sequence of elements
c(1:16),
# No of rows
nrow = 4,
byrow = TRUE
)
print("Matrix")
print(mat)
# calculating the off-diagonal elements
ele = mat[row(mat)!=col(mat)]
# calculating the minimum of these
# elements
print("Mean of off diagonal elements")
mean(ele)
Output
[1] "Matrix"
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[1] "Mean of off diagonal elements"
[1] 8.5
Similar Reads
Find row and column index of maximum and minimum value in a matrix in R In this article, we will discuss how to find the maximum and minimum value in any given matrix and printing its row and column index in the R Programming language. Example: Input: 11 -9 36 20 1 81 13 99 77 Output: maximum value: 99 row col 3 2 minimum value: -9 row col 1 2Finding Maximum value:In th
4 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 Values Above Main Diagonal of a Matrix in R 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 mai
3 min read
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
Find Indices of Maximum and Minimum Value of Matrix in MATLAB Matrices in MATLAB are 2-dimensional arrays that store mostly numeric data at different indices. Now, to find the indices of maximum and minimum values of a given matrix, MATLAB does not provide any direct functionality however, we can do the same by using two other functionalities. Firstly, we will
3 min read
Convert an Object into a Matrix in R Programming - as.matrix() Function The as.matrix() function within R converts objects of various classes into a matrix. This can be helpful to work with structures of various data that can be converted into the matrix structure so that it becomes easier to analyze.Syntax: as.matrix(x)Parameters: x: Object to be convertedExample 1: Co
2 min read