R Programming - Find Row and Column Index of Max/Min values in a Matrix
R Programming: Matrix Exercise-11 with Solution
Write a R program to find row and column index of maximum and minimum value in a given matrix.
Sample Solution:
R Programming Code:
# Create a 4x4 matrix with values from 1 to 16, filled by row
m = matrix(c(1:16), nrow = 4, byrow = TRUE)
# Print a message indicating that the following output is the original matrix
print("Original Matrix:")
# Print the original matrix
print(m)
# Find the row and column indices of the maximum value in the matrix
result = which(m == max(m), arr.ind=TRUE)
# Print a message indicating that the following output is the row and column of the maximum value
print("Row and column of maximum value of the said matrix:")
# Print the indices of the maximum value
print(result)
# Find the row and column indices of the minimum value in the matrix
result = which(m == min(m), arr.ind=TRUE)
# Print a message indicating that the following output is the row and column of the minimum value
print("Row and column of minimum value of the said matrix:")
# Print the indices of the minimum value
print(result)
Output:
[1] "Original 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] "Row and column of maximum value of the said matrix:" row col [1,] 4 4 [1] "Row and column of minimum value of the said matrix:" row col [1,] 1 1
Explanation:
- Create a Matrix:
- m = matrix(c(1:16), nrow = 4, byrow = TRUE)
- Creates a 4x4 matrix named m with values from 1 to 16, filling the matrix by rows.
- Print Original Matrix:
- print("Original Matrix:")
- Prints the label "Original Matrix:" to indicate that the following output is the original matrix.
- print(m)
- Prints the content of the matrix m.
- Find Maximum Value Index:
- result = which(m == max(m), arr.ind=TRUE)
- Finds the indices (row and column) of the maximum value in the matrix m.
- arr.ind=TRUE returns the indices in array format.
- Print Maximum Value Indices:
- print("Row and column of maximum value of the said matrix:")
- Prints the label indicating that the following output shows the row and column indices of the maximum value.
- print(result)
- Prints the indices of the maximum value.
- Find Minimum Value Index:
- result = which(m == min(m), arr.ind=TRUE)
- Finds the indices (row and column) of the minimum value in the matrix m.
- arr.ind=TRUE returns the indices in array format.
- Print Minimum Value Indices:
- print("Row and column of minimum value of the said matrix:")
- Prints the label indicating that the following output shows the row and column indices of the minimum value.
- print(result)
- Prints the indices of the minimum value.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to convert a given matrix to a list of column-vectors.
Next: Write a R program to rotate a given matrix 90 degree clockwise rotation.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/r-programming-exercises/matrix/r-programming-matrix-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics