I have matrix, I want to write a function, get the element of matrix and return me the coordinate of number inside of matrix. could somebody give an idea how to implement it ?
> A
[,1] [,2]
[1,] 10 20
[2,] 21 17
[3,] 13 25
[4,] 21 11
[5,] 31 24
for example
myfunction(11)
> 11
> row 3, col 1
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.
Theorem Any vector space V has a basis. If V has a finite basis, then all bases for V are finite and have the same number of elements (called the dimension of V). v = x1v1 + x2v2 + ··· + xnvn, where xi ∈ R. The coefficients x1,x2,...,xn are called the coordinates of v with respect to the ordered basis v1,v2,...,vn.
In linear algebra, a coordinate vector is a representation of a vector as an ordered list of numbers that describes the vector in terms of a particular ordered basis. Coordinates are always specified relative to an ordered basis.
which()
takes an argument, arr.ind=TRUE
, which will return the indices of all TRUE
elements in a logical matrix to which it is applied.
## An example matrix
set.seed(1)
m <- matrix(sample(1:100, 10), ncol=2)
m
# [,1] [,2]
# [1,] 27 86
# [2,] 37 97
# [3,] 57 62
# [4,] 89 58
# [5,] 20 6
## An example application
which(m==58, arr.ind=TRUE)
# row col
# [1,] 4 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With