从 MATLAB 中的时间序列中删除重复值

Removing duplicate values from a timeseries in MATLAB

我有一个看起来像这样的时间序列:

Time    Data
-------------
802     1
803     2
803     3
804     7

我想使用 interp(x,v,xq),x 作为时间,v 作为数据,但该函数要求我有不同的 x 值。我如何 remove/average 在 MATLAB 时间序列中复制行?

我试过了interp(unique(timeseriesname.time), timeseriesname.data, timeseriesref.time)

但这与我的数据不匹配。有没有简单的方法可以做到这一点?

如果您只想对数据 as-is 进行插值,您可以尝试 resample method for timeseries 对象。当然,对于您给出的示例,它会在时间点 803 附近显示不连续性,但它仍然可以正常工作而不会出现错误:

>> ts = timeseries([1 2 3 7].', [802 803 803 804]);
>> rs = resample(ts, 802:0.25:804);
>> [rs.time rs.data]

ans =

  802.0000    1.0000
  802.2500    1.2500
  802.5000    1.5000
  802.7500    1.7500
  803.0000    3.0000
  803.2500    4.0000
  803.5000    5.0000
  803.7500    6.0000
  804.0000    7.0000

注意时间点 803 如何具有值 3,但插值从下方接近值 2

如果您只想通过平均删除重复条目,您可以使用函数 unique and accumarray 创建一个新的 timeseries 对象,如下所示:

>> [newTime, ~, index] = unique(ts.time);
>> newTS = timeseries(accumarray(index, ts.data, [], @mean), newTime);
>> [newTS.time newTS.data]

ans =

  802.0000    1.0000
  803.0000    2.5000
  804.0000    7.0000

或者,如果您想修改原始 timeseries对象的时间和数据,您可以与set同时设置它们方法:

[newTime, ~, index] = unique(ts.time);
newData = accumarray(index, ts.data, [], @mean);
set(ts, 'Time', newTime, 'Data', newData);