Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

given value of matrix, getting it's coordinate

Tags:

r

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
like image 294
user2806363 Avatar asked Nov 19 '13 01:11

user2806363


People also ask

How do you find the coordinate of a 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.

How do you find the coordinates of a vector with respect to a basis?

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.

What is a coordinate vector linear algebra?

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.


1 Answers

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
like image 152
Josh O'Brien Avatar answered Nov 15 '22 07:11

Josh O'Brien