Skip to content

MAtrix addressing

1 message · David Winsemius

#
Begin forwarded message:
> mtx <- matrix(seq(1:36), nrow=6, byrow=TRUE,  
dimnames=list(x=seq(-1, 1, length=7)[-7], y=seq(-1, 1, length=7)[-7]) )
 > mtx
                    y
x                    -1 -0.666666666666667 -0.333333333333333  0  
0.333333333333333 0.666666666666667
  -1                  1                  2                  3   
4                 5                 6
  -0.666666666666667  7                  8                  9  
10                11                12
  -0.333333333333333 13                 14                 15  
16                17                18
  0                  19                 20                 21  
22                23                24
  0.333333333333333  25                 26                 27  
28                29                30
  0.666666666666667  31                 32                 33  
34                35                36

 > fnfind <- function(x,y) mtx[ findInterval(x,  
c(as.numeric(rownames(mtx)), 1)),
+                              findInterval(y,  
c(as.numeric(colnames(mtx)), 1))]
 > fnfind(.5,.5)
[1] 29
 > fnfind(-.5,-.5)
[1] 8

This could obviously be made more compact, but the current form allows  
simple modification of the length and endpoints of x and y.
David Winsemius, MD
West Hartford, CT