Skip to content
Prev 320816 / 398500 Next

arrayInd and which

Folks,

I have Googled but not found much regarding arrayInd aside from the "which" help page.

Any good examples or docs on what arrayInd does that is better or different from which()?

In addition take the following 20x10 matrix:

td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 
6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 
6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 
3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2, 
6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6, 
6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 
4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L
))

I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That is my bounds are by column.

Is there a better way to do this other than:

bounds<-c(rep(5,5), rep(4,5))
idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE)
row col
 [1,]   1   1
 [2,]  13   1
 [3,]  13   2
 [4,]   1   3
 [5,]   8   3
 [6,]  13   3
 [7,]   1   4
 [8,]  13   4
 [9,]   1   5
[10,]  13   5
[11,]   1   6
[12,]   4   6
[13,]  13   6
[14,]   4   7
[15,]  13   7
[16,]   1   8
[17,]   4   8
[18,]  13   8
[19,]   3   9
[20,]   1  10
[21,]  13  10

Lastly can you explain these results:
[1] 4 6
[1] 4
[1] 6
Error: subscript out of bounds

Thanks in advance for your help,
KW

--