MATLAB: Comparing 2 arrays with different lengths

陌路散爱 提交于 2019-12-01 10:58:35

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!