Alex
Alex

Reputation: 97

How can you retrieve values from 3D arrays at the same 2D location?

Let's say I have an array given by

A = array(1:8,c(2,2,2))
ind = which(A[,,1]>=2)

which yields

[1] 2 3 4

Now, how do I access the 2,3,4 values at each level in the third dimension (so, 2,3,4,6,7,8), but not 5?
A[ind,] obviously doesn't work...

Upvotes: 0

Views: 47

Answers (1)

timfaber
timfaber

Reputation: 2070

ind = which(A[,,c(1,2)]>=2,arr.ind=T)
A[ind]

EDIT

Get the index of one array from a multidimensional array:

A = array(1:8,c(2,2,2))
ind = which(A[,,1]>=2,arr.ind=T)
apply(A, 3, function(x) x[ind])

Upvotes: 1

Related Questions