如何使Matlab fillmissing函数仅在已知值之间估算一定数量的缺失值?
我们仅出于示例目的考虑此代码:
A = [NaN NaN NaN NaN 9; NaN NaN 2 5 7; NaN 3 4 NaN 9; 11 NaN 12 NaN 14; 44 5 15 12 nan];
dates = datetime({'2010','2011','2012','2013','2014'},'InputFormat','yyyy')';
TT = array2timetable(A,'RowTimes',dates);
我想使用 matlab 函数 fillmissing 根据以下规则插补缺失数据:
- 时间序列开头的缺失数据不应 估算
- 时间序列末尾的 缺失数据不应 仅当满足以下情况时,才应对已知值内的估算
- 缺失数据进行估算 已知值之间的缺失值数量非常少 大于 2
请注意,此处仅插补了 A2 列中的第 4 行。我可以用 fillmissing 来做到这一点吗?不然我怎么能这么做呢?
Let's consider this code only for exemplification purpose:
A = [NaN NaN NaN NaN 9; NaN NaN 2 5 7; NaN 3 4 NaN 9; 11 NaN 12 NaN 14; 44 5 15 12 nan];
dates = datetime({'2010','2011','2012','2013','2014'},'InputFormat','yyyy')';
TT = array2timetable(A,'RowTimes',dates);
I would like to use the matlab function fillmissing to impute missing data according to the following rules:
- missing data at the beginning of the time series should not be
imputed - missing data at the end of the time series should not be
imputed - missing data within known values should be imputed only if
the number of missing values between known values is strictly minor
than 2
The resulting timetable should be:
Notice that only the 4th row in the column A2 has been imputed here. Can I do that with fillmissing? Otherwise how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
find
查找第一个和最后一个非 NaN 值。根据这些指标,如果缺失值少于 2 个,您可以有条件地填充缺失数据。对于某些向量v
:对于数组
A
,您可以循环遍历列并应用上面的内容,其中每一列都是一个向量v
。You can find the first and last non-NaN values using
find
. Based on these indicies, you can conditionally fill missing data if there are fewer than 2 missing values. For some vectorv
:For your array
A
you can loop over the columns and apply the above, where each column is a vectorv
.