Skip to content
Prev 248389 / 398502 Next

MAtrix addressing

The reason is the following image
http://img545.imageshack.us/i/maptoregion.jpg/
In the picture above you will find the indexes for each cell.

Also you will see that I place that matrix inside a x,y region that spans from -1 to 1. I am trying to write one function that will get as argument a (x,y) value x e[-1,1] y e[-1,1] and will return the indexes of that cell tha x,y value correspond to.

I really do not have a clue how I should try to approach that to solve it. So based on some version I had for 1-d vector I tried to extend it for 2-d. I used findInterval as a core to get results.
Unfortunately my code fails to produce accurate results as my approach 'assumes' (this is something inhereted by the find Interval function) that the numbering starts bottom left and goes high top right.
You will find my code below




sr.map <- function(sr){
# This function converts the s(x,y) matrix into a function x that spans #from -1 to 1 and y spans from -1 to 1.
# Input: sr a x,y matrix containing the shadowing values of a Region
     breaksX <- seq(from=-1, to = 1, length = nrow(sr) +1L )
     breaksY <- seq(from=-1, to = 1, length = ncol(sr) + 1L)
     function(x,y){ # SPAGGETI CODE FOR EVER
         indx <- findInterval(x, breaksX,rightmost.closed=TRUE)
	 indy <- findInterval(y, breaksY,rightmost.closed=TRUE)
	 c(indx,indy)
     }
 }



sr<-matrix(data=seq(from=1,to=36),nrow=6,ncol=6,byrow=TRUE)
f.sr.map<-sr.map((sr))
f.sr.map(-0.1,-0.1)
f.sr.map(0.1,0.1)



Best Regards
Alex
--- On Wed, 1/26/11, David Winsemius <dwinsemius at comcast.net> wrote: