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
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. >
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. >
>>>>> "GaGr" == Gabor Grothendieck <ggrothendieck at gmail.com> >>>>> on Thu, 16 Aug 2007 23:46:28 -0400 writes:GaGr> Get the indices using expand.grid and then reorder GaGr> them: set.seed(1); X <- array(rnorm(24), 2:4) # input GaGr> X # look at X GaGr> do.call(expand.grid, sapply(dim(X), seq))[order(X),] Excellent, Gabor! Definitely the nicest of the solutions so far! GaGr> 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