MATLAB:比较2个具有不同长度的数组 [英] MATLAB: Comparing 2 arrays with different lengths
问题描述
我需要比较两个具有不同长度(由于采样率不同)的数组.我想对较大的数组进行下采样以匹配较小的数组,但是该因素不是整数而是十进制.
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 length, however the factor is not an integer but a decimal.
例如:
a =
1 1.375 1.75 2.125 2.5 2.875 3.25
b =
1 2 3
是否有任何方法可以操纵这些数组以匹配长度?
Is there any way to manipulate these arrays to match lengths?
推荐答案
聪明地使用 interp1
.诀窍在于,用于插值的关键点是一个数组,该数组从1到与a
中的值一样多,我们将称为N
,并且插值的关键点将是一个线性增加的数组,其中第一个点是1,最后一个点是N
,您可以将此范围平均划分为与b
中的点一样多.
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
.
简单地说:
anew = interp1(1:numel(a), a, linspace(1, N, numel(b)));
linspace
生成从1到N = numel(a)
的点数尽可能多,我们将其确定为b
中的元素总数.这恰好指定了您想要给与b
的长度匹配的a
的下采样版本的正确关键点,尽管将需要一些插值.默认的插值方法是线性.
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.
使用您提供的a
的示例输入,我们得到:
Using the sample input from a
you provided, we get:
>> anew
anew =
1.0000 2.1250 3.2500
这篇关于MATLAB:比较2个具有不同长度的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!