Find row and column index of maximum and minimum value in a matrix in R

• Last Updated : 21 Apr, 2021

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   2

Finding Maximum value:

• In the code below, we have created a sample matrix, in which we have passed “nrow=3“(matrix will have only 3 rows) in example 1 and  “ncol=2“(matrix will have only 2 columns) in example 2.
• Then we have printed the sample matrix in the next line with the message “Sample Matrix”.
• Then we have used the syntax below to find the row and column number of the maximum element and stored it in the variable “max”. We have made use of the max() function which is used to find the maximum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.
• Thewhich()” function is used to get the index or position of the value which satisfies the given condition. Then we have printed the maximum value along with its row and column index.

Syntax: which(m == max(m), arr.ind=TRUE)

Example 1:

R

 # defining a sample matrixm = matrix(c(11, 20, 13, -9, 1, 99, 36, 81, 77),            nrow = 3)    print("Sample Matrix:")print(m)  # stores indexes of max value max = which(m == max(m), arr.ind = TRUE)  print(paste("Maximum value: ", m[max]))print(max)

Output:

 "Sample Matrix:"
[,1] [,2] [,3]
[1,]   11   -9   36
[2,]   20    1   81
[3,]   13   99   77

 "Maximum value:  99"
row col
[1,]   3   2

Example 2:

R

 # defining a sample matrixm = matrix(c(1:16), ncol = 2)  print("Sample Matrix:")print(m)  # stores indexes of max valuemax = which(m == max(m), arr.ind=TRUE)   print(paste("Maximum value: ",m[max]))print(max)

Output:

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

 "Maximum value:  16"
row col
[1,]   8   2

Finding Minimum value:

• In the code below, we have created a sample matrix, in which we have passed “nrow=3“(matrix will have only 3 rows) in example 1 and  “ncol=8“(matrix will have only 8 columns) in example 2 as a parameter while defining the matrix.
• Then we have printed the sample matrix in the next line with the message “Sample Matrix”.
• Then we have used the syntax below to find the row and column number of the minimum element and stored it in the variable “min”. We have made use of the min() function which is used to find the minimum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.
• The “which()” function is used to get the index or position of the value which satisfies the given condition. Then we have printed the minimum value along with its row and column index.

Syntax: which(m == min(m), arr.ind=TRUE)

Example 1:

R

 # defining a sample matrixm = matrix(c(11, 20, 13, -9, 1, 99, 36, 81, 77), nrow = 3)  print("Sample Matrix:")print(m)  # stores indexes of min valuemin = which(m == min(m), arr.ind = TRUE)  print(paste("Minimum value: ", m[min]))print(min)

Output:

 "Sample Matrix:"
[,1] [,2] [,3]
[1,]   11   -9   36
[2,]   20    1   81
[3,]   13   99   77

 "Minimum value:  -9"
row col
[1,]   1   2

Example 2:

R

 # defining a sample matrixm = matrix(c(1:16), ncol = 8)  print("Sample Matrix:")print(m)  # stores indexes of min value min = which(m == min(m), arr.ind = TRUE) print(paste("Minimum value: ", m[min]))print(min)

Output:

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

 "Minimum value:  1"
row col
[1,]   1   1

My Personal Notes arrow_drop_up