64081

using interp1 in R for matrix

I am trying to use the interp1 function in R for linearly interpolating a matrix without using a for loop. So far I 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 for 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() for (i in 1:2){ t <- Temp[i,] layerT[[i]] <- interp1(depthTemp,t,layerZ) } layerT <- do.call(rbind,layerT)

So, here I have used interp1 on each row of the matrix in a for loop. I would like to know how I could do this without using a for loop. I can do this in matlab by transposing the matrix as follows:

layerT = interp1(depthTemp,Temp',layerZ)'; % matlab code

but when I attempt to do this in R

layerT <- interp1(depthTemp,t(Temp),layerZ)

it does not return a matrix of interpolated results, but a numeric array. How can I ensure that R returns a matrix of the interpolated values?

Answer1:

    <li>

    There is nothing wrong with your approach; I probably would avoid the intermediate t <-

    </li> <li>

    If you want to feel R-ish, try

    </li> </ul>

    apply(Temp,1,function(t) interp1(depthTemp,t,layerZ))

    You may have to add a t(ranspose) in front of all if you really need it that way.

    Since this is a 3d-field, per-row interpolation might not be optimal. My favorite is interp.loess in package tgp, but for regular spacings other options might by available. The method does not work for you mini-example (which is fine for the question), but required a larger grid.

Recommend

  • Canvas for drawing is a circle; random dots within canvas
  • Core animation stops when the Master of the split viewcontroller hide (in portrait mode)?
  • Image scaling geometry
  • Dimension issue with scipy's curve_fit function
  • Remove parenthesis and text in a file using Python
  • Access NSArray from a NSTimer Interval = EXC_BAD_ACCESS
  • SQLite HAVING comparison error
  • UITableViewCell change selectedBackgroundView alpha?
  • How to move object along the polygons
  • How to fix this floating point square root algorithm
  • Graph matplotlib to show total count in the histogram bins
  • Count the number of non-null values in a Spark DataFrame
  • Visual Studio 2010 - 2015 does not use ymm* registers for AVX optimization
  • Update data in d3.js group
  • Why doesn't CGContextSetStrokeColorWithColor set text colour to black?
  • Converting float[,] to list in f#?
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Extract data between rows r
  • How to send control C to Mac Terminal using python?
  • NSMutableArray instance used in a block
  • XOR with Neural Networks (Matlab)
  • pymongo replication secondary readreference not work
  • triggering user space with kernel
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • How can I include If-None-Match header in HttpRequestMessage
  • Calculating ratio of reciprocated ties for each node in igraph
  • How to create CGPath from a SKSpriteNode in SWIFT
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Generate random number from custom distribution
  • Force show.bind execution
  • Mysterious problem with floating point in LISP - time axis generation
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Why winpcap requires both .lib and .dll to run?
  • how to add data labels for bar graph in matlab
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • reshape alternating columns in less time and using less memory
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app