How can I sweep the first two dimensions of an array based on the first columns of third dimension?
A simplified example:
My array:
a <- array(1:24,c(4,3,2))
> a
, , 1
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
, , 2
[,1] [,2] [,3]
[1,] 13 17 21
[2,] 14 18 22
[3,] 15 19 23
[4,] 16 20 24
I would like to divide each matrix by its first column by sweep (or another) function. I would like to put the following output in a single array by using a single sweep function.
> sweep(a[,,1], 1, a[,,1][,1], "/")
[,1] [,2] [,3]
[1,] 1 5.000000 9.000000
[2,] 1 3.000000 5.000000
[3,] 1 2.333333 3.666667
[4,] 1 2.000000 3.000000
> sweep(a[,,2], 1, a[,,2][,1], "/")
[,1] [,2] [,3]
[1,] 1 1.307692 1.615385
[2,] 1 1.285714 1.571429
[3,] 1 1.266667 1.533333
[4,] 1 1.250000 1.500000
How can I achieve this?