# ---- Default case ------------------------------------
x <- array(1:27 + 2, rep(3,3))
# find index of `x` equal to either 4 or 5
fwhich(x, c(4,5))
res <- fwhich(x, c(4,5), ret.values = TRUE)
res
attr(res, "values")
# ---- file-array case --------------------------------
arr <- filearray_create(tempfile(), dim(x))
arr[] <- x
fwhich(arr, c(4,5))
fwhich(arr, c(4,5), arr.ind = TRUE, ret.values = TRUE)
arr[2:3, 1, 1]
# Clean up this example
arr$delete()
# ---- `val` is a function ----------------------------
x <- as_filearray(c(sample(15), 15), dimension = c(4,4))
ret <- fwhich(x, val = which.max,
ret.values = TRUE, arr.ind = FALSE)
# ret is the index
ret == which.max(x[])
# attr(ret, "values") is the max value
max(x[]) == attr(ret, "values")
# customize `val`
fwhich(x, ret.values = TRUE, arr.ind = FALSE,
val = function( slice ) {
slice > 10 # or which(slice > 10)
})
Run the code above in your browser using DataLab