Open In App

Min, Max and Mean of Off-Diagonal Elements in a Matrix in R

Improve
Improve
Like Article
Like
Save
Share
Report

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


Last Updated : 02 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads