在 Matlab 中使用給定數組對值進行線性化的快速方法

在 Matlab 中使用給定數組對值進行線性化的快速方法

我正在尋找一種快速方法來線性化 Matlab 中值之間的值。

例子:

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

答案1

// 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)

做你想做的事。

此向量a包含要在規則間隔的座標上插值的值,其範圍從 1 到 的長度a。為此,可以使用 Matlab 函數interp1,該函數在給定支撐點(第一個參數)、這些支撐點上的值(第二個參數)和請求的插值座標(第三個參數)的情況下執行線性插值。但是,根據 OP 的請求,透過簡短的特定函數呼叫進行插值,該函數f允許在特定座標(或座標向量)處插值向量a,只要它們保持在 range 內[1,length(a)]

相關內容