interpolation - using interp1 in R for matrix -
i trying use interp1 function in r linearly interpolating matrix without using loop. far have tried:
bthd <- c(0,2,3,4,5) # original depth vector btha <- c(4000,3500,3200,3000,2800) # original array of area temp <- c(4.5,4.2,4.2,4,5,5,4.5,4.2,4.2,4) temp <- matrix(temp,2) # matrix temperature measurements # -- interpolating bathymetry data -- depthtemp <- c(0.5,1,2,3,4) layerz <- seq(depthtemp[1],depthtemp[5],0.1) library(signal) layera <- interp1(bthd,btha,layerz); # -- interpolate= matrix -- layert <- list() (i in 1:2){ t <- temp[i,] layert[[i]] <- interp1(depthtemp,t,layerz) } layert <- do.call(rbind,layert)
so, here have used interp1 on each row of matrix in loop. know how without using loop. can in matlab transposing matrix follows:
layert = interp1(depthtemp,temp',layerz)'; % matlab code
but when attempt in r
layert <- interp1(depthtemp,t(temp),layerz)
it not return matrix of interpolated results, numeric array. how can ensure r returns matrix of interpolated values?
there nothing wrong approach; avoid intermediate
t <-
if want feel r-ish, try
apply(temp,1,function(t) interp1(depthtemp,t,layerz))
you may have add t(ranspose) in front of if need way.
since 3d-field, per-row interpolation might not optimal. favorite interp.loess
in package tgp
, regular spacings other options might available. method not work mini-example (which fine question), required larger grid.
Comments
Post a Comment