[R] Map an Area to another
Alaios
alaios at yahoo.com
Tue Jan 25 13:34:20 CET 2011
Dear All,
I would like to ask you help with the following:
Assume that I have an area of 36 cells (or sub-areas)
sr<-matrix(data=seq(from=1,to=36),nrow=6,ncol=6,byrow=TRUE)
> sr
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12
[3,] 13 14 15 16 17 18
[4,] 19 20 21 22 23 24
[5,] 25 26 27 28 29 30
[6,] 31 32 33 34 35 36
This 6*6 matrix denotes an area and the contents of the cell denote the values the area gets inside that cell-zone (or sub-area).
I would like to map this AREA into x,y function for x e[-1,1] ye[-1,1]
I really didn't know how to implement that, so with help I got from a list member I tried to use FindInterval for the x,y plane. I am sure that it might be an easier way to do that so please do suggest it. If there is not then continue reading.
In the picture below:
http://img521.imageshack.us/f/maparea.gif/
You can see the matrix presented above placed inside the x e[-1,1] ye[-1,1] which from now on these are the results of the f(x,y) function.
I.e f(-0.5,0.3) will return the appropriate cell (x,y) index.
As I told you I tried using the findInterval to get for the f(x,y) as an output the correct cell number.
This is what I have implemented so far (you can copy paste the code there is a small call of my function at the end):
sr.map <- function(sr){
# This function converts the s(x,y) matrix into a function x 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)
if ( (x<0) && (y>0) ) # x<0,y>0
c(indx,ncol(sr)-indy+1)
else if ( (x>0) && (y>0) ) # x>0,y>0
c(nrow(sr)-indx+1,indy)
else if ( (x<0) && (y<0) ) # x>0,y<0
c(nrow(sr)-indx+1,indy)
else if ( (x>0) && (y<0) ) # x>0,y<0
c(indx,ncol(sr)-indy+1)
else
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)
If you are worried about my so many if inside the main body of my function you can find why in the picture above. In the right part of the images you can see some of my calculations
Top right part: What I wanted to have and what was the output was when there was no if statement but only the last line c(indx,indy) (what two findIntervals returned)
Based on the what I wanted (column on the top right part) I wrote the changes in the logic by introducing if statements. You can find them at the bottom right part.
This works fine for values close to the 'borders'
f.sr.map(-1,-1)
f.sr.map(-1,1)
f.sr.map(1,-1)
f.sr.map(1,1)
but not for values for example close to zero. This is a wrong output
f.sr.map(0.1,1)
[1] 3 6
I am sure that it might be an easier way to do all that so please inform me .
I would like to thank all of you for your kind efforts to help me
Best Regards
Alex
More information about the R-help
mailing list