[R] apply on a 4D array

Thomas W Blackwell tblackw at umich.edu
Thu Sep 25 20:39:59 CEST 2003


Ben  -

I think you want something like

new <- array(prod(hiaAry, probAry[,,,1], probAry[,,,2],
           probAry[,,,3], probAry[,,,4]), dim(hiaAry))

But I'm just guessing.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Thu, 25 Sep 2003 Benjamin.STABLER at odot.state.or.us wrote:

> I am trying to multiply a 3D array of 4x4x4 by the 4 3D arrays of a 4D array
> with dimensions 4x4x4x4 (the last dimension being the one that I want to
> split by).
>
> (4x4x4 array)
> > hiaAry
> , , a1
>
>           i1       i2      i3        i4
> h1 9.5936098 6.001040 0.08772 0.3138600
> h2 1.2003500 1.454570 2.79248 0.0000000
> h3 0.1346500 0.201220 0.39256 0.5464000
> h4 0.0109000 0.012270 0.16417 0.2766900
>
> , , a2
>
>           i1          i2       i3       i4
> h1 195.08275 74.23508453 39.23165 14.89689
> h2   1.80127  7.41649055  6.97972  2.77784
> h3   0.00000  0.27444002  0.77353  6.10322
> h4   0.01554  0.01232000  0.40669  3.69832
>
> , , a3
>
>          i1          i2      i3      i4
> h1 25.81161 13.57603931 6.40551 1.38103
> h2  2.61562  2.26407003 2.13906 1.41652
> h3  0.05780  0.30406001 1.15228 0.00000
> h4  0.00000  0.04258000 0.22346 0.43209
>
> , , a4
>
>          i1       i2         i3      i4
> h1 59.47618 16.54495 0.00000000 0.00000
> h2  1.50265  2.76623 0.00000000 0.08917
> h3  0.00000  0.42722 0.00000000 1.29230
> h4  0.00000  0.00000 0.07564002 0.34235
>
>
> (4x4x4x4 array)
> dim "wX" is the dimension that I want to split the array by.
> > probAry
> , , a1, w1
>
>           i1         i2         i3          i4
> h1 0.3784811 0.17321659 0.11236581 0.157227931
> h2 0.2569944 0.08635110 0.03812272 0.036192191
> h3 0.1659365 0.04928920 0.01856106 0.015686506
> h4 0.0939886 0.02551057 0.00846568 0.006614069
>
> , , a2, w1
>
>           i1         i2          i3          i4
> h1 0.3227855 0.14088002 0.090150394 0.127416178
> h2 0.2181195 0.07210281 0.032273293 0.031203848
> h3 0.1409469 0.04170138 0.015971840 0.013690984
> h4 0.0804619 0.02189455 0.007375192 0.005817293
>
> , , a3, w1
>
>           i1        i2         i3        i4
> h1 0.7558389 0.5157448 0.39155036 0.4867531
> h2 0.6611814 0.3640772 0.20821663 0.2126003
> h3 0.5494104 0.2540337 0.11997469 0.1092745
> h4 0.4100844 0.1566448 0.06143343 0.0507088
>
> , , a4, w1
>
>           i1        i2        i3        i4
> h1 0.9445381 0.8542088 0.7797483 0.8391612
> h2 0.9234338 0.7957714 0.6755850 0.7172834
> h3 0.8941212 0.7257939 0.5631331 0.5815922
> h4 0.8487089 0.6284291 0.4226977 0.4107244
>
> , , a1, w2................................
>
> I thought it would be something along the lines of apply(4Darray, c(1,2,3),
> function(x) x*3Darray).  But this returns the results in the form of 64rows
> by dim1 by dim2 by dim3.
>
> , , i4, a4
>                 h1           h2           h3           h4
>  [1,]   8.05058486 6.881337e+00 5.579569e+00  3.940329195
>  [2,]   0.19306289 2.185652e-01 2.347175e-01  0.219540239
>  .
>  .
>  .
> [63,]   0.00000000 1.300468e-01 2.430320e-01  0.395577205
> [64,]   0.00000000 0.000000e+00 1.191559e-02  0.034329350
>
> It looks like the rows are all the combinations of the 4th dimension with
> the other dimensions.  So then I thought I could array it back to the
> dimensions I want and maybe use aperm if I needed to.  But there must be a
> better way to do this.  I can of course just multiple the 3D array by each
> 3D cube of the 4D array with a loop...but that would be inefficient.
>
> I thought maybe sweep(4Darray, 4, 3Darray, "*"), but that returns different
> results than multiplying each 3D array by each 3D cube of the 4D array.  I
> guess what I am asking is for some clarification on apply and sweep.  I have
> read the manuals and searched the list, but I can't seem to find much about
> operations with 3 or more dimensions.  Thanks.
>
> Ben Stabler
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>




More information about the R-help mailing list