function to find coodinates in an array
3
0
Entering edit mode
Ana Conesa ▴ 130
@ana-conesa-2246
Last seen 10.2 years ago
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
• 1.5k views
ADD COMMENT
0
Entering edit mode
@moshe-olshansky-2329
Last seen 10.2 years ago
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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
ADD COMMENT
0
Entering edit mode
See arrayIndex() in the R.utils package, e.g. X <- array((2*3*4):1, dim=c(2,3,4)) idx <- 1:length(X) ijk <- arrayIndex(idx, dim=dim(X)) print(ijk) [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 1 1 [3,] 1 2 1 [4,] 2 2 1 [5,] 1 3 1 [6,] 2 3 1 [7,] 1 1 2 [8,] 2 1 2 [9,] 1 2 2 10,] 2 2 2 11,] 1 3 2 12,] 2 3 2 13,] 1 1 3 14,] 2 1 3 15,] 1 2 3 16,] 2 2 3 17,] 1 3 3 18,] 2 3 3 19,] 1 1 4 20,] 2 1 4 21,] 1 2 4 22,] 2 2 4 23,] 1 3 4 24,] 2 3 4 /Henrik On 8/16/07, Moshe Olshansky <m_olshansky at="" yahoo.com=""> 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 > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
ADD REPLY
0
Entering edit mode
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 > >
ADD REPLY
0
Entering edit mode
[Ana Conesa] > 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 [Moshe Olshansky] >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) The reshape package might be helpful, here. If I understand the problem correctly, given this artificial example: X <- sample(1:24) dim(X) <- c(2, 3, 4) you would want: library(reshape) melt(X)[order(X), -4] so getting the indices in a three columns data frame. -- Fran?ois Pinard http://pinard.progiciels-bpi.ca
ADD REPLY
0
Entering edit mode
@lgautieralternorg-747
Last seen 10.2 years ago
> 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? ...cross-posting is generally not advised. Identify where to post first is generally considered good practice. In case no one on R-help came with a solution, here is something that appears working (in toy examples): # the easy one first Xx <- array(rep(seq(1, prod(dim(X)[1])), prod(dim(X)[c(2,3)])), dim(X)) # then making the other ones easy Xy <- local({ X <- aperm(X, c(2, 1, 3)) Xy <- array(rep(seq(1, prod(dim(X)[1])), prod(dim(X)[c(2,3)])), dim(X)) aperm(Xy, c(2, 1, 3)) }) Xz <- local({ X <- aperm(X, c(3, 2, 1)) Xz <- array(rep(seq(1, prod(dim(X)[1])), prod(dim(X)[c(2,3)])), dim(X)) aperm(Xz, c(3, 2, 1)) }) # what you want: Xx[ord] Xy[ord] Xz[ord] L. > Thanks! > > Ana > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor >
ADD COMMENT
0
Entering edit mode
@gabor-grothendieck-2332
Last seen 10.2 years ago
Get the indices using expand.grid and then reorder them: set.seed(1); X <- array(rnorm(24), 2:4) # input X # look at X do.call(expand.grid, sapply(dim(X), seq))[order(X),] On 8/16/07, 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
ADD COMMENT

Login before adding your answer.

Traffic: 636 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6