Quick way to linearize values using given array in Matlab

0

I'm looking for a quick way to linearize a value between values in Matlab.

Example:

a = ([10 20 30 40])
index = 1.5 //a float index
func(a,index); //shall return a value between index 1 and 2. In this case would be the value 15.
Ans = 15

Cleber Marques

Posted 2017-03-22T18:23:45.997

Reputation: 103

Answers

2

// define a function that interpolates a vector 'a' defined on a regular grid
// at interpolated support coordinates 'x'
f = @(a, x) interp1( 1:length(a), a, x);

// test vector (given by OP)
a=[10 20 30 40];
// this vector interpolated at coordinate 1.5 gives 15
// (can be a vector of coordinates)
f(a, 1.5)

does what you want.

The vector a contains the values to be interpolated on a regularly spaced coordinates that range from 1 to the length of a. To achieve that, one can use the Matlab function interp1, which performs a linear interpolations given support points (first argument), values on these support points (second argument) and requested interpolation coordinates (third argument). However, as per OP's request to do the interpolation with a short specific function call, this function f allows to interpolate the vector a at a specific coordinate (or vector of coordinates) as long as they keep within the range [1,length(a)].

Sander

Posted 2017-03-22T18:23:45.997

Reputation: 156