![在 Matlab 中使用给定数组线性化值的快速方法](https://linux22.com/image/1525565/%E5%9C%A8%20Matlab%20%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%BB%99%E5%AE%9A%E6%95%B0%E7%BB%84%E7%BA%BF%E6%80%A7%E5%8C%96%E5%80%BC%E7%9A%84%E5%BF%AB%E9%80%9F%E6%96%B9%E6%B3%95.png)
我正在寻找一种快速方法来线性化 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
在特定坐标(或坐标向量)处插入向量,只要它们保持在 范围内[1,length(a)]
。