You will get prod(dim(M)-1) 3-dimensional arrays. What do you want
to do with these 3-d arrays? At least at the prototype stage, I might
use "for" loops.
> M <- array(1:27, dim = c(3, 3, 3))
> M.sum <- array(NA, dim = dim(M) - 1)
> for(i in 1:2)
for(j in 1:2)
for(k in 1:2)
M.sum[i, j, k] <- M[ - c(i, i + 1), - c(j, j + 1),
- c(k, k + 1)]
> M
, , 1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
, , 2
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
, , 3
[,1] [,2] [,3]
[1,] 19 22 25
[2,] 20 23 26
[3,] 21 24 27
> M.sum
, , 1
[,1] [,2]
[1,] 27 21
[2,] 25 19
, , 2
[,1] [,2]
[1,] 9 3
[2,] 7 1
hope this helps. spencer graves
Alain Yamakana wrote:
Dear All,
I would be very appreciative of your help with the following. Given a 3-D
matrix M I would like to perform all submatrices
M[-c(i,i+1),-c(j,j+1),-c(k,k+1)] for i in 1:nrow(M), j in 1:ncol(M), and k
in 1:dim(M)[3] AND keep track of all combinations (1,1,1), (1,1,2),
(1,1,3),
etc. I tried expand.grid() function without success on a 2-D matrix.
Regards, AY
|