在Matlab中针对不同的数据集折叠/平均数据 [英] Collapse/mean data in Matlab with respect to a different set of data
问题描述
我有两组数据,但是两组数据的大小不同. 每个集合都包含测量本身(MeasA和MeasB,均为双精度)和测量发生时的时间点(TimeA和TimeB,datenum或朱利安日期).
I have two sets of data, but the sets have a different sizes. Each set contains the measurements itself (MeasA and MeasB, both double) and the time point (TimeA and TimeB, datenum or julian date) when the measuring happened.
现在,我想将较小的数据集与较大的数据集进行匹配,并且要做到这一点,我想表示围绕数据响应的较大数据集的数据点.较小集合的时间点,最后进行一些相关性分析.
Now I want to match the smaller data set to the bigger one, and to do this, I want to mean the data points of the bigger set around the data resp. time points of the smaller set, to finally do some correlation analysis.
小示例,数据看起来如何:
Small Example how the data would look like:
MeasA = [2.7694 -1.3499 3.0349 0.7254 -0.0631];
TimeA = [0.2 0.4 0.7 0.8 1.3];
MeasB = [0.7147 -0.2050 -0.1241 1.4897 1.4090 1.4172 0.6715 -1.2075 0.7172 1.6302];
TimeB = [0.1 0.2 0.3 0.6 0.65 0.68 0.73 0.85 1.2 1.4];
现在我想折叠MeasB和TimeB,以使测量的平均值接近TimeA中的时间点,因此,例如TimeB应该看起来像这样:
And now I want to collapse MeasB and TimeB so that I get the mean of the measurement close to the timepoints in TimeA, so for example TimeB should look like this:
TimeB = [mean([0.1 0.2]) mean([0.3 0.6]) mean([0.65 0.68 0.73]) mean([0.85]) mean([1.2 1.4])]
TimeB = [0.15 0.4 0.69 0.85 1.3]
然后也像这样折叠MeasB:
And then collapse MeasB like this too:
MeasB = [mean([0.7147 -0.2050]) mean([-0.1241 1.4897]) mean([1.4090 1.4172 0.6715]) mean([-1.2075]) mean([0.7172 1.6302])];
MeasB = [0.2549 0.6828 1.1659 -1.2075 1.1737]
推荐答案
函数 interp1
是您的朋友.
The function interp1
is your friend.
您可以使用以下方法同时为集合A
获取一组新的测量值:
You can get a new set of measurement for your set B
, at the same time than set A
by using:
newMeasB = interp1( TimeB , MeasB , TimeA ) ;
前2个参数是您要重新插值的集合的原始时间和测量值,最后一个参数是希望在其上计算插值的新x轴(在您的示例中为时间).
The first 2 parameters are your original Time and Measurements of the set you want to re interpolate, the last parameter is the new x axis (time in your example) on which you want the interpolated values to be calculated.
这样,您就不会在两组测量之间得到不同的时间集,可以逐点进行比较.
This way you do not end up with different sets of time between your 2 sets of measurements, you can compare them point by point.
查看 interp1
的文档,以获取更多说明和有关内插或任何潜在外插的选项.
Check the documentation of interp1
for more explanations and for options about the interpolation or any potential extrapolation.
Matlab的文档曾经对功能进行了很好的说明,但是我无法在线找到它,所以可以这样做:
edit: Matlab doc used to have a great illustration of the function but I can't find it online so here goes:
因此,使用linear
方法时,如果将值精确地插值到2个点之间,则该函数将返回精确的均值.如果内插比另一个点更接近一点,则返回的值将成比例地接近最接近点的值.
So with the linear
method, if the value is interpolated exactly between 2 points, the function will return the exact mean. If the interpolation is done closer to one point than another, the value returned will be proportionally closer to the value of the closest point.
如果TimeA
没有被timeB
完全重叠,则NaN
可能出现在侧面(返回向量的开头或结尾).该函数无法内插",因为没有锚点.但是,interp1
的不同选项允许您在输入范围之外外推",或分配另一个默认值而不是NaN.
The NaN
can appear on the sides (beginning or end of returned vector) if the TimeA
was not completely overlapped by timeB
. The function cannot "interpolate" because there is no anchor point. However, the different options of interp1
allow you to "extrapolate" outside of the input range, or to assign another default value instead of the NaNs.
这篇关于在Matlab中针对不同的数据集折叠/平均数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!