[R] Fwd: MAtrix addressing

David Winsemius dwinsemius at comcast.net
Wed Jan 26 14:50:02 CET 2011



Begin forwarded message:

> From: David Winsemius <dwinsemius at comcast.net>
> Date: January 26, 2011 8:32:30 AM EST
> To: Alaios <alaios at yahoo.com>
> Subject: Re: [R] MAtrix addressing
>
>
> On Jan 26, 2011, at 7:58 AM, Alaios wrote:
>
>> Unfortunately right now is convoluted... by I was trying to find  
>> some solution.
>> Bring again this picture in front of u
>> http://img545.imageshack.us/i/maptoregion.jpg/
>>
>> Consider f a function that gets as input the coords
>> so
>> f(-1,-1) should return the value of the bottom left point
>> f(1,1) should return the value of the top right point.
>>
>>
>> A.For me an area is approximated by a matrix so each cell of the  
>> matrix corresponds to a fixed value in a small sub-area.
>> So When my function gets coords like f(0,0.3) should find the  
>> corresponding sub-area.
>>
>> B. This is want to do
>> I also have a data structure called matrix that in every cell has  
>> the appropriate values of that area.
>>
>> A+B => Combine these two and have a function that returns the  
>> appropriate value of a subarea given its coords. Attention my area  
>> spans from -1 to 1 in y plane and from -1 to 1 in the x plane.
>
 > 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.


>>
>> Was it clearer this way? (Why is always so hard to me to explain  
>> even simple tasks?)
>>
>> Regards
>> Alex
>>
>> --- On Wed, 1/26/11, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>>> From: David Winsemius <dwinsemius at comcast.net>
>>> Subject: Re: [R] MAtrix addressing
>>> To: "Alaios" <alaios at yahoo.com>
>>> Cc: R-help at r-project.org
>>> Date: Wednesday, January 26, 2011, 12:49 PM
>>>
>>> On Jan 26, 2011, at 2:47 AM, Alaios wrote:
>>>
>>>> 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
>>>
>>> If one wants to take an ordinary r matrix and reorder it in
>>> the manner you describe:
>>>
>>> mtx2 <- mtx[ nrow(mtx):1, ]
>>>
>>> Whether that is an efficient way to get at the sokution
>>> your you programming task I cannot say. It sounds as though
>>> it has gotten too convoluted. I was not able to comprehend
>>> the overall goal from your problem description.
>>>
>>>>
>>>>
>>>>
>>>>
>>>> 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:
>>>>
>>>>> From: David Winsemius <dwinsemius at comcast.net>
>>>>> Subject: Re: [R] MAtrix addressing
>>>>> To: "Alaios" <alaios at yahoo.com>
>>>>> Cc: R-help at r-project.org
>>>>> Date: Wednesday, January 26, 2011, 2:54 AM
>>>>>
>>>>> On Jan 25, 2011, at 4:50 PM, Alaios wrote:
>>>>>
>>>>>> Hello
>>>>>> I would like to ask you if it is possible In R
>>> Cran to
>>>>> change the default way of addressing a matrix.
>>>>>> for example
>>>>>> matrix(data=seq(from=1,to=4,nrow=2,ncol=2, by
>>> row
>>>>> numbering) # not having R at this pc
>>>>>>
>>>>>> will create something like the following
>>>>>> 1 2
>>>>>> 3 4
>>>>>>
>>>>>> the way R address this matrix is from top left
>>> corner
>>>>> moving to bottom right.
>>>>>> The cell numbers in that way are
>>>>>> 1 2
>>>>>> 3 4
>>>>>>
>>>>>> IS it possible to change this default
>>> addresing number
>>>>> to something that goes bottom left to top right?
>>> In this
>>>>> simple case I want to have
>>>>>> 3 4
>>>>>> 1 2
>>>>>>
>>>>>> Would that be possible?
>>>>>
>>>>> Yes. it's possible but ... why?
>>>>>
>>>>>>
>>>>>> I would like to thank y for your help
>>>>>> Regards
>>>>>> Alex
>>>>>>
>>>>>>
>>> ______________________________________________
>>>>>> R-help at r-project.org
>>>>> mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal,
>>> self-contained,
>>>>> reproducible code.
>>>>>
>>>>> David Winsemius, MD
>>>>> West Hartford, CT
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>>
>>
>>
>>
>
> David Winsemius, MD
> West Hartford, CT
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list