在数组中每个元素之间使用MATLAB linspace [英] Using MATLAB linspace between every element in an array
问题描述
使用MATLAB,我想在数组的每个点之间进行线性插值.
Using MATLAB, I would like to linearly interpolate between every point in an array.
使用interpolate
将以非线性方式进行.我想做的事情类似于产生低通滤波器系数.
Using interpolate
will do it in a non-linear fashion. What I want to do is similar to producing low-pass filter coefficients.
我想出了一个解决方案,但我想避免使用for
循环:
I have come up with a solution, but I would like to avoid using for
loops:
a=[0 0 1 0 0]; %Input matrix
N=5; %Number of points to be added
b=[];
for i=1:length(a)-1
b=[b linspace(a(i),a(i+1),N)];
end
有没有可能做到这一点?
Is it possible to do this without a loop?
推荐答案
您可以使用a
中的点作为控制点来创建线性样条线.之后,您可以指定从开始间隔到结束间隔的任意多个点.就像Raab70所说的那样,您可以使用interp1
.可以通过以下方式(使用linear
插值)调用interp1
:
You can create a linear spline with the points in a
as control points. After that you can specify as many points as you want from a beginning interval to an ending interval. As what Raab70 said, you can use interp1
. interp1
can be called in the following way (using linear
interpolation):
out = interp1(x, y, xp, 'linear')
x
是控制点的x
值,y
是控制点的y
值. xp
是要在其上评估功能的点.因为您没有明确地具有x
值,而只想要y
值,所以可以创建一个从1到L
的虚拟矢量,其中L
是输出数据中的点数.该伪向量用于您的x
值.之后,将y
指定为输出数据.在这种情况下,它是a
的数组.接下来,您需要指定沿曲线采样的位置.因为要在每个空间之间引入5个点,所以总共总共有5 * 4 + 5 = 25个点.每个插槽" 5个点,并且总共有4个插槽.您还包括来自原始输出数据的5个点.要创建这25个点,只需在1到5之间执行linspace
,然后指定在此间隔之间要25个点即可.这样可以完美地捕获控制点(1,2,3,4,5
的虚拟值)以及要用于在每个控制点之间进行插值的值.
x
are the x
values and y
are the y
values for the control points. xp
are the points you want to evaluate the function at. Because you don't explicitly have x
values and you just want the y
values, you can create a dummy vector that goes from 1 up to L
where L
is how many points are in your output data. This dummy vector is for your x
values. After, specify y
as your output data. In this case, it is the array of a
. Next, you need to specify where you want to sample along the curve. Because you want to introduce 5 points in between each space, you will have in total 5*4 + 5 = 25 points all together. 5 points per "slot", and there are 4 slots all together. You also include the 5 points that were from your original output data. To create these 25 points, simply perform linspace
from 1 up to 5 and specify that you want 25 points in between this interval. This should perfectly capture the control points (the dummy values of 1,2,3,4,5
) as well as the values that you want to use to interpolate in between each of the control points.
这样,尝试这样的事情:
As such, try something like this:
N = 5; %// Number of points to introduce in between each control point
y = [0 0 1 0 0]; %// Your output data
L = numel(y); %// Size of output data. Cache so we don't have to keep typing in numel(y)
x = 1:L; %// Dummy vector
xp = linspace(1, L, N*(L-1) + N); %// Create points to interpolate. N*(L-1) + N is also just N*L
out = interp1(x, y, xp, 'linear'); %// Generate interpolated array
out
因此给了我
out =
Columns 1 through 9
0 0 0 0 0 0 0 0.1667 0.3333
Columns 10 through 18
0.5000 0.6667 0.8333 1.0000 0.8333 0.6667 0.5000 0.3333 0.1667
Columns 19 through 25
0 0 0 0 0 0 0
这篇关于在数组中每个元素之间使用MATLAB linspace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!