What would experienced R developers consider the most efficient (yet still readable) way to construct a matrix with a given number of rows and columns from a given function, such that e.g. A_ij = someFun(i,j) with 1 <= i <= rows, 1 <= j <= cols?
Since I couldn’t find something in the documentation I came up with
initMatrix <- function(rows, cols, fn) {
A <- matrix(nrow=rows, ncol=cols)
for (i in 1:rows)
for (j in 1:cols)
A[i,j] <- fn(i,j)
return(A)
}
which seems silly and slow to me. Any improvements (particularly one-liners) welcome! 🙂
I think you’re looking for
outer(seq(rows),seq(cols),fn)(or as suggested below,outer(seq_len(rows),seq_len(cols),fn): would need some examples to see how much difference that made).You can gain a lot in readability (at least if you don’t have to go look up
?outerto find out what’s going on) this way, but I don’t actually think you save much time. Something cleverer and more efficient might be possible if yourfnis vectorized to begin with: is it?