s-news
[Top] [All Lists]

Re: All combinations of dimensions

To: Alain Yamakana <alain.yamakana@rogers.com>
Subject: Re: All combinations of dimensions
From: Spencer Graves <spencer.graves@pdf.com>
Date: Sun, 29 Feb 2004 17:54:32 -0800
Cc: "Snews (E-mail)" <s-news@lists.biostat.wustl.edu>
In-reply-to: <004101c3ff2c$37c4e3c0$1d503141@bloor.phub.net.cable.rogers.com>
References: <004101c3ff2c$37c4e3c0$1d503141@bloor.phub.net.cable.rogers.com>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
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



<Prev in Thread] Current Thread [Next in Thread>