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.
- The "which()" 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 matrix
m = 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:
[1] "Sample Matrix:"
[,1] [,2] [,3]
[1,] 11 -9 36
[2,] 20 1 81
[3,] 13 99 77
[1] "Maximum value: 99"
row col
[1,] 3 2
Example 2:
R
# defining a sample matrix
m = matrix(c(1:16), ncol = 2)
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:
[1] "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
[1] "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 matrix
m = matrix(c(11, 20, 13, -9, 1, 99, 36, 81, 77), nrow = 3)
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:
[1] "Sample Matrix:"
[,1] [,2] [,3]
[1,] 11 -9 36
[2,] 20 1 81
[3,] 13 99 77
[1] "Minimum value: -9"
row col
[1,] 1 2
Example 2:
R
# defining a sample matrix
m = 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:
[1] "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
[1] "Minimum value: 1"
row col
[1,] 1 1
Similar Reads
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
How to find number of rows and columns in a matrix in R In this article, we will see how to return the total number of rows and columns in a matrix in R Programming Language. What is Matrix?In R programming, Matrix is a two-dimensional, homogeneous data structure in which rows and columns run horizontally. 1. Getting the number of rowsnrow() function is
3 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
Find Column with Maximum Zeros in Matrix Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with the maximum number of 0s. If more than one column exists, print the one which comes first. If the maximum number of 0s is 0 then return -1. Examples: Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1},
5 min read
Get the Minimum and Maximum element of a Vector in R Programming - range() Function range() function in R Programming Language is used to get the minimum and maximum values of the vector passed to it as an argument. Syntax: range(x, na.rm, finite) Parameters:Â x: Numeric Vectorna.rm: Boolean value to remove NAfinite: Boolean value to exclude non-finite elementsR - range() Function
1 min read
Getting a Matrix of number of columns in R Programming - col() Function col() function in R Language is used to get a matrix which contains the number of columns of the matrix passed to it as argument. Syntax: col(x, as.factor=FALSE) Parameters: x: matrix as.factor: a logical value indicating whether the value should be returned as a factor of column labels (created if
2 min read