Using MATLAB linspace between every element in an array

前端 未结 1 1388
长发绾君心
长发绾君心 2021-01-20 06:42

Using MATLAB, I would like to linearly interpolate between every point in an array.

Using interpolate will do it in a non-linear fashion. What I want t

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 07:15

    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 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 thus gives me:

    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
    

    0 讨论(0)
提交回复
热议问题