0

I got some questions about linking 2 matrix in R.

I got a matrix with depth associated to latitudes (rows, i) and longitudes (col, j) within a lagoon. Here is an example of the data :

      [,1] [,2] [,3]
[1,] 2.28 3.80 4.55
[2,] 1.35 5.70 2.88
[3,] 3.79 4.40 3.24

I got a list of couples of coordinates (i,j) indicating areas of interest within the lagoon. For example :

1,2, 3,3, 2,1 

etc.

What I need to do is to replace the coordinates of the list by the corresponding depth values of the first matrix.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Doc Martin's
  • 341
  • 1
  • 3
  • 11

1 Answers1

1
 mat <- matrix( c(2.28, 3.80, 4.55,
                  1.35, 5.70, 2.88,
                  3.79 ,4.40 ,3.24),      3, byrow=TRUE)
 idxmat <- matrix( c(1,2, 3,3, 2,1 ), 3, byrow=TRUE)
 mat[idxmat]
#[1] 3.80 3.24 1.35

Using a 2 column-matrix to extract values from another matrix is a standard R maneuver. I'm sure it's been asked and answered (by me for one) before, but I suspect that searching for the strategy might be difficult.

IRTFM
  • 258,963
  • 21
  • 364
  • 487