[BioC] [R] function to find coodinates in an array

Marc Schwartz marc_schwartz at comcast.net
Fri Aug 17 15:07:51 CEST 2007


Ana,

Two quick comments:

1. Needless to say, my use of 'ARR[order(ARR)]' can of course be
simplified to 'sort(ARR)'. For some reason, I got fixated on your use of
order() last night.

2. I suspect that you will find Gabor's solution to be superior to mine.
Most notably because I neglected to consider that your array elements
might be floating point values, in which case, the equality used in
which() will fail in cases where floating point values cannot be exactly
represented. A workaround would be to use all.equal(), but it makes for
more complicated code with respect to looping. Gabor's code will be data
type independent and therefore more generally viable.

Marc

On Fri, 2007-08-17 at 08:25 +0200, Ana Conesa wrote:
> The arr.ind in the which function does the job very nicely!!!
> Thank you everyone for the  suggestions!
> 
> Ana
> 
> >
> >
> >---- Mensaje Original ----
> >De: marc_schwartz at comcast.net
> >Para: m_olshansky at yahoo.com
> >Asunto: Re: [BioC] [R] function to find coodinates in an array
> >Fecha: Thu, 16 Aug 2007 21:53:44 -0500
> >
> >>If I am correctly understanding the problem, I think that this is
> >what
> >>you want:
> >>
> >>set.seed(1)
> >>
> >># Create a 3x3x3 array
> >>ARR <- array(sample(100, 27), c(3, 3, 3))
> >>
> >>> ARR
> >>, , 1
> >>
> >>     [,1] [,2] [,3]
> >>[1,]   27   89   97
> >>[2,]   37   20   62
> >>[3,]   57   86   58
> >>
> >>, , 2
> >>
> >>     [,1] [,2] [,3]
> >>[1,]    6   61   43
> >>[2,]   19   34   88
> >>[3,]   16   67   83
> >>
> >>, , 3
> >>
> >>     [,1] [,2] [,3]
> >>[1,]   32   17   21
> >>[2,]   63   51   29
> >>[3,]   75   10    1
> >>
> >>
> >># Get the ordered indices of the elements in the array
> >>> order(ARR)
> >> [1] 27 10 24 12 22 11  5 25  1 26 19 14  2 16 23  3  9 13  8 20 15
> >21
> >>[23] 18  6 17  4  7
> >>
> >>
> >># Get the actual array elements in order
> >>> ARR[order(ARR)]
> >> [1]  1  6 10 16 17 19 20 21 27 29 32 34 37 43 51 57 58 61 62 63 67
> >75
> >>[23] 83 86 88 89 97
> >>
> >>
> >># Now loop over the above and using which(), get the 3D indices
> >>> t(sapply(ARR[order(ARR)], function(x) which(ARR == x, arr.ind =
> >TRUE)))
> >>      [,1] [,2] [,3]
> >> [1,]    3    3    3
> >> [2,]    1    1    2
> >> [3,]    3    2    3
> >> [4,]    3    1    2
> >> [5,]    1    2    3
> >> [6,]    2    1    2
> >> [7,]    2    2    1
> >> [8,]    1    3    3
> >> [9,]    1    1    1
> >>[10,]    2    3    3
> >>[11,]    1    1    3
> >>[12,]    2    2    2
> >>[13,]    2    1    1
> >>[14,]    1    3    2
> >>[15,]    2    2    3
> >>[16,]    3    1    1
> >>[17,]    3    3    1
> >>[18,]    1    2    2
> >>[19,]    2    3    1
> >>[20,]    2    1    3
> >>[21,]    3    2    2
> >>[22,]    3    1    3
> >>[23,]    3    3    2
> >>[24,]    3    2    1
> >>[25,]    2    3    2
> >>[26,]    1    2    1
> >>[27,]    1    3    1
> >>
> >>
> >>See ?which and take note of the arr.ind argument.
> >>
> >>HTH,
> >>
> >>Marc Schwartz
> >>
> >>
> >>On Thu, 2007-08-16 at 19:21 -0700, Moshe Olshansky wrote:
> >>> A not very good solution is as below:
> >>> 
> >>> If your array's dimensions were KxMxN and the "linear"
> >>> index is i then
> >>> n <- ceiling(i/(K*M))
> >>> i1 <- i - (n-1)*(K*M)
> >>> m <- ceiling(i1/K)
> >>> k <- i1 - (m-1)*K
> >>> 
> >>> and your index is (k,m,n)
> >>> 
> >>> I am almost sure that there is a function in R which
> >>> does this (it exists in Matlab).
> >>> 
> >>> Regards,
> >>> 
> >>> Moshe.
> >>> 
> >>> --- Ana Conesa <aconesa at ochoa.fib.es> wrote:
> >>> 
> >>> > Dear list,
> >>> > 
> >>> > I am looking for a function/way to get the array
> >>> > coordinates of given
> >>> > elements in an array. What I mean is the following:
> >>> > - Let X be a 3D array
> >>> > - I find the ordering of the elements of X by ord <-
> >>> > order(X) (this
> >>> > returns me a vector)
> >>> > - I now want to find the x,y,z coordinates of each
> >>> > element of ord
> >>> > 
> >>> > Can anyone help me?
> >>> > 
> >>> > Thanks!
> >>> > 
> >>> > Ana
> >>> >



More information about the Bioconductor mailing list