[R] extracting values from a 3d array using a matrix from indices

Tamas Papp tpapp at axelero.hu
Wed Mar 31 16:51:56 CEST 2004

Suppose I have A, an n x m matrix, each element is an integer (an

I also have B, an n x l x m array.  I need C, where

C[n,m] = B[n, A[n, m], m]

I am currently using loops, what would be the "R way" to do this?

Another question: let

A[n, m] <- argmax_l B[n, l, m]

what would be the nicest way of doing this?  Currently I am using
max.col and a single loop, going though the n's.

Background: I solving a discrete-space dynamic programming problem, A
is the optimal policy, C is the value function.  The structure of the
problem allows me to use matrices like above, instead of (n * m) x (n
x m) square matrices.



Tamás K. Papp
E-mail: tpapp at axelero.hu (preferred, especially for large messages)
        tpapp at westel900.net
Please try to send only (latin-2) plain text, not HTML or other garbage.

More information about the R-help mailing list