I am currently using this implementation:
getVal = function(i, x, margin) {
rst = ifelse(margin==1, x[i, ], x[, i])
}
i.e. return the i-th row OR column of x, depending on the value of margin.
=== update ===
Just realized my usage of ifelse(x,y,z) statement here is wrong, as it returns a value the same length as its first argument. My implementation of getVal should have:
...
rst = if (margin == 1) x[i, ] else x[, i]
...
abind::asub()does something very much like what you’re trying to do (and also generalizes nicely to higher-dimensional arrays). Itsidxanddimsarguments correspond, respectively, to youriandmarginarguments.