indexing - Which inputs produced the ith output of outer in R? -
r has function outer allows compute function every combination of inputs xs , ys, e.g.:
xs <- 1:5 ys <- 0:10 zs <- outer(xs, ys, vectorize(function(x, y) sin(x) + y**2)) let's want know minimal value in zs:
> min(zs) [1] -0.9589243 and position it's in:
> which(zs==min(zs)) [1] 5 now how can figure out x of xs , y of ys produced 5th z of zs?
i can recompute using outer(xs, ys, vectorize(function(x, y) paste(x,y)))[which(zs==min(zs))] string of indices, seems bloody inefficient me. what's better way?
you have use:
which(zs==min(zs), arr.ind=t) # row col #[1,] 5 1
Comments
Post a Comment