- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the coordinate of a value in an R matrix?
The coordinate of a value in an R matrix is the row and column intersection that is the row and column index for that particular value. This can be found by using which function.
For example, if we have a matrix called M that contains value starting from 1 to 20 then we can find the coordinate of value 5 by using the command given below −
which(M==5,arr.ind=TRUE)
Example
Following snippet creates a matrix −
M1<-matrix(rpois(80,10),ncol=4) M1
The following matrix is created −
[,1][,2][,3][,4] [1,] 6 16 10 11 [2,] 10 4 15 10 [3,] 5 16 14 8 [4,] 8 11 14 13 [5,] 15 13 10 8 [6,] 10 11 6 13 [7,] 2 11 13 11 [8,] 6 16 15 10 [9,] 3 7 14 7 [10,] 8 4 10 11 [11,] 9 6 15 10 [12,] 14 12 11 10 [13,] 13 8 10 6 [14,] 7 13 11 4 [15,] 8 7 11 12 [16,] 12 13 9 12 [17,] 10 8 6 9 [18,] 3 11 8 9 [19,] 9 6 11 12 [20,] 10 18 12 9
To find the coordinates of value 11 in M1, add the following code to the above snippet −
M1<-matrix(rpois(80,10),ncol=4) which(M1==11,arr.ind=TRUE)
Output
If you execute all the above given snippets as a single program, it generates the following output −
row col [1,] 4 2 [2,] 6 2 [3,] 7 2 [4,] 18 2 [5,] 12 3 [6,] 14 3 [7,] 15 3 [8,] 19 3 [9,] 1 4 [10,] 7 4 [11,] 10 4
Example 2
Following snippet creates a matrix −
M2<-matrix(rpois(80,2),ncol=4) M2
The following matrix is created −
[,1][,2][,3][,4] [1,] 2 0 1 2 [2,] 1 1 1 2 [3,] 1 3 0 1 [4,] 3 1 8 3 [5,] 1 6 1 2 [6,] 2 2 2 1 [7,] 3 3 0 1 [8,] 3 1 1 1 [9,] 4 2 3 3 [10,] 4 1 0 3 [11,] 3 3 3 1 [12,] 3 2 5 1 [13,] 4 4 4 3 [14,] 3 5 4 2 [15,] 2 0 3 2 [16,] 1 2 5 2 [17,] 1 1 3 3 [18,] 2 3 4 1 [19,] 3 3 2 2 [20,] 4 1 3 0
To find the coordinates of value 4 in M2, add the following code to the above snippet −
M2<-matrix(rpois(80,2),ncol=4) which(M2==4,arr.ind=TRUE)
Output
If you execute all the above given snippets as a single program, it generates the following output −
row col [1,] 9 1 [2,] 10 1 [3,] 13 1 [4,] 20 1 [5,] 13 2 [6,] 13 3 [7,] 14 3 [8,] 18 3
Example 3
Following snippet creates a matrix −
M3<-matrix(rpois(40,5),ncol=2) M3
The following matrix is created −
[,1][,2] [1,] 7 6 [2,] 6 7 [3,] 4 3 [4,] 5 5 [5,] 6 7 [6,] 4 5 [7,] 4 6 [8,] 6 4 [9,] 6 4 [10,] 8 1 [11,] 8 5 [12,] 5 13 [13,] 1 1 [14,] 5 5 [15,] 4 8 [16,] 6 5 [17,] 6 7 [18,] 9 6 [19,] 5 6 [20,] 4 8
To find the coordinates of value 8 in M3, add the following code to the above snippet −
M3<-matrix(rpois(40,5),ncol=2) which(M3==8,arr.ind=TRUE)
Output
If you execute all the above given snippets as a single program, it generates the following output −
row col [1,] 10 1 [2,] 11 1 [3,] 15 2 [4,] 20 2
- Related Articles
- How to find the maximum value in each matrix stored in an R list?
- How to find the row and column position of a value as vector in an R matrix?
- How to find the maximum value for each column of a matrix in R?
- How to find the inverse of a matrix in R?
- How to find the rank of a matrix in R?
- How to find the moving standard deviation in an R matrix?
- How to find power of a matrix in R?
- How to find the row product of a matrix in R?
- Find the column index of least value for each row of an R matrix
- How to find the mean of columns of an R data frame or a matrix?
- How to find the position of a non-NA value in an R vector?
- How to find the row-wise mode of a matrix in R?
- Python – 3D Matrix to Coordinate List
- How to find the combination of matrix values in R?
- How to add a value to a particular matrix element in R?
