interpolation - How to use R interp1 similarly as MATLAB's? -
i trying interpolate linearly in r. pseudocode u = interp1(u, linspace(1, numel(u), numel(u)-1));
in matlab extrapolation returns nan if point outside domain (default, more here).
approx rule=1
equivalent matlab pseudocode
i not sure second interp1
parameter not required in matlab let unsuccessufully y <- x
such
interp1(x, y, xi, method = "linear")
minimal code example (real 1 has > 500 k points linear work) , output @ top
list of 2 $ : num [1:3] 1 2 3 $ : num [1:2] 1 2 num [1:2] 0 1 error in interp1(x, y, xi, method = "linear") : points 'xi' outside of range of argument 'x'. execution halted library("pracma") # http://finzi.psych.upenn.edu/library/pracma/html/interp1.html files <- vector("list", 2) files[[1]] <- c(1,2,3) files[[2]] <- c(1,2) str(files) # wanted, matlab: u = interp1(u, linspace(1, numel(u), numel(u)-1)); xi <- seq(0,1, len = length(files[[1]]) - 1) x <- files[[1]] y <- files[[1]] str(xi) files[[1]] <- interp1(x, y, xi, method = "linear") str(files)
i know thread using interp1 in r matrix not have matrix.
input: c(1,2,3)
expected output: [1:2] datastructure
r: 3.3.1
os: debian 8.5
if you're willing na
values on extrapolation, as default linear interpolation/extrapolation in interp1, approx()
works fine:
files <- list(1:3,1:2) xi <- seq(0,1, len = length(files[[1]]) - 1) x <- files[[1]] y <- files[[1]] <- approx(x,y,xi)
you said wanted two-element vector presumably want output y-values:
a$y ## [1] na 1
this may seem wrong, correct answer question posed. you've used files[[1]]
both x
, y
, approx()
should return y=x when input in range 1 3, , na
otherwise. in case xi
[0 1]
, first element out of range of x/y data provided ...
ps can appreciate wanting use pracma
similarity matlab's syntax, - although pracma
high-quality , used - base r functions more used/thoroughly tested ...
Comments
Post a Comment