 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- 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
