Search code examples
rapply

apply involving array and matrix


Suppose I have data, P, that is a 3x3x10 array and a matrix, q, that is a 10x3 matrix. A function, pv_fun, takes P[,,i] as an input and q[i,] as an input:

P=array(runif(90,0,1),c(3,3,10))
q=matrix(runif(30,0,1),nrow = 10)

pv.fun<-function(Prob,Prev){
  solve(diag(as.vector(Prob%*%Prev)))%*%Prob%*%diag(Prev)
} 

This works if I specify "i"

pv.fun(P[,,1],q[1,])
           [,1]      [,2]       [,3]
[1,] 0.02192327 0.6626384 0.31543835
[2,] 0.52426958 0.4473938 0.02833658
[3,] 0.34857273 0.5157891 0.13563820

However, I would like to avoid a loop and use apply, but I can't figure out how.

If I try: apply(P,3,pv.fun,q)

This is close, i think: I'm telling it to use the 3rd dimension of P but how to I tell 'apply' to use the 1st dimension of q?

Thanks in advance!

Rich


Solution

  • How about using lapply() instead of apply(). The apply() way would work if you could use all of q for each iteration of apply, but since you need to subset P and q, that seems like a less useful strategy.

    P=array(runif(90,0,1),c(3,3,10))
    q=matrix(runif(30,0,1),nrow = 10)
    
    pv.fun<-function(Prob,Prev){
      solve(diag(as.vector(Prob%*%Prev)))%*%Prob%*%diag(Prev)
    } 
    
    
    lapply(1:nrow(q), function(i)pv.fun(P[,,i], q[i, ]))
    #> [[1]]
    #>           [,1]      [,2]      [,3]
    #> [1,] 0.1492837 0.2669265 0.5837899
    #> [2,] 0.1502058 0.5824942 0.2673000
    #> [3,] 0.1411805 0.2513680 0.6074515
    #> 
    #> [[2]]
    #>            [,1]      [,2]       [,3]
    #> [1,] 0.08494298 0.6144880 0.30056900
    #> [2,] 0.03750454 0.7719693 0.19052613
    #> [3,] 0.19968033 0.7864584 0.01386132
    #> 
    #> [[3]]
    #>           [,1]      [,2]        [,3]
    #> [1,] 0.4515211 0.5451417 0.003337122
    #> [2,] 0.2979098 0.7018926 0.000197589
    #> [3,] 0.4403383 0.5592538 0.000407891
    #> 
    #> [[4]]
    #>           [,1]       [,2]      [,3]
    #> [1,] 0.4184959 0.04292074 0.5385834
    #> [2,] 0.2976478 0.05667290 0.6456793
    #> [3,] 0.4141699 0.12402696 0.4618032
    #> 
    #> [[5]]
    #>           [,1]      [,2]      [,3]
    #> [1,] 0.4212735 0.1986239 0.3801026
    #> [2,] 0.1233982 0.3057945 0.5708073
    #> [3,] 0.5879795 0.1914091 0.2206114
    #> 
    #> [[6]]
    #>           [,1]        [,2]      [,3]
    #> [1,] 0.3336499 0.257089986 0.4092602
    #> [2,] 0.6231942 0.047247936 0.3295578
    #> [3,] 0.4067584 0.009297672 0.5839439
    #> 
    #> [[7]]
    #>            [,1]      [,2]      [,3]
    #> [1,] 0.05933416 0.5847652 0.3559006
    #> [2,] 0.12551019 0.7238791 0.1506108
    #> [3,] 0.08715145 0.6421023 0.2707463
    #> 
    #> [[8]]
    #>           [,1]      [,2]      [,3]
    #> [1,] 0.5773402 0.1198203 0.3028394
    #> [2,] 0.6856290 0.1707220 0.1436490
    #> [3,] 0.2166357 0.3480572 0.4353071
    #> 
    #> [[9]]
    #>           [,1]      [,2]      [,3]
    #> [1,] 0.3454159 0.5091042 0.1454799
    #> [2,] 0.2020375 0.6085781 0.1893844
    #> [3,] 0.3496506 0.5355888 0.1147606
    #> 
    #> [[10]]
    #>             [,1]      [,2]       [,3]
    #> [1,] 0.008277708 0.8252787 0.16644363
    #> [2,] 0.017052752 0.9246549 0.05829239
    #> [3,] 0.029653306 0.7013757 0.26897095
    

    Created on 2023-03-02 with reprex v2.0.2