> "GaGr" == Gabor Grothendieck <[EMAIL PROTECTED]>
> 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(di
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 <[EMAIL PROTECTED]> wrote:
> Dear list,
>
> I am looking for a function/way to get the array coordin
[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 coordin
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
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,]111
[2,]211
[3,]121
[4,]221
[5,]131
[6,]231
[7,]1
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 Mat
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 ele