MATLAB: Comparing 2 arrays with different lengths

前端 未结 1 1141
难免孤独
难免孤独 2021-01-14 22:55

I have two arrays with different lengths (due to different sampling rates) that I need to compare. I\'d like to downsample the larger array to match the smaller one in lengt

相关标签:
1条回答
  • 2021-01-14 23:32

    That's easy to do with clever use of interp1. The trick is that the keypoints used for interpolation is an array going from 1 up to as many values as you have in a which we will call N, and the interpolated keypoints would be a linearly increasing array where the first point is 1, the last point is N and you evenly divide up this range to have as many points as there are in b.

    Simply put:

    anew = interp1(1:numel(a), a, linspace(1, N, numel(b)));
    

    linspace generates a linearly increasing array from 1 to N = numel(a) for as many points as you want, which we determine as the total number of elements in b. This exactly specifies the right keypoints you want to give you a downsampled version of a that matches the length of b, though there will be some interpolation required. The default interpolation method is linear.

    Using the sample input from a you provided, we get:

    >> anew
    
    anew =
    
        1.0000    2.1250    3.2500
    
    0 讨论(0)
提交回复
热议问题