【问题标题】:How to remove table data based on comparison between different columns in another table in MATLAB?如何根据MATLAB中另一个表中不同列之间的比较来删除表数据?
【发布时间】:2018-11-28 10:36:50
【问题描述】:

我在 MATLAB 表 A 和表 B 中有 2 个表,每个表都有不同的维度(不同的行数和列数)。表 A 的第一列有日期和时间,格式类似于 2018-11-01 12:00:00(DateTime 数据格式)。

现在,在表 B 中,第三列和第四列还包含日期和时间,格式类似于 2018-11-01 01:11:12:173000。我想要实现的是从表 A 中删除所有行(这是数据实例),如果表 A 的日期时间落在表 B 中的日期和时间之间的范围内。(更准确地说,假设表B在第一行/第一个数据实例的第三列中有一个日期时间条目为2018-11-10 12:30:00:173,在第四列为2018-11-10 12:40:00:145,我想从表A中删除所有数据条目/行,以防万一例如,表 A 的 DateTime Column 值在2018-11-10 12:30:00:1732018-11-10 12:40:00:145 的范围内)。这意味着基本上我会从表 A 中删除上述范围内的数据。

要解决这个问题,我首先想到的是使用 inner join(),但是,从 Mathworks 社区指南中可以明显看出,innerjoin() 仅匹配我指定 Key 的确切列值,但在在这种情况下,我会在表 B 的 2 列中查看一系列 DateTime 值,所以这可能不是最好的方法。为此目的使用for loop 也可能有效,但会非常复杂和冗余,因为表中的大数据需要大量计算时间。非常感谢您在这方面的任何帮助。

【问题讨论】:

标签: matlab datetime matlab-table


【解决方案1】:

我在 Mathworks 社区页面上收到了一些很好的问题答案,可以在 https://uk.mathworks.com/matlabcentral/answers/432509-how-to-remove-table-data-based-on-comparison-between-different-columns-in-another-table-in-matlab?s_tid=prof_contriblnk 找到答案

我很欣赏 Guillaume 的回答(在上面提到的 Mathworks 社区页面链接上回答)并将其放在这里以供将来对任何人的帮助:-

%inputs: TableA with a column named date, TableB with a column named datestart and dateend
%replace by actual table and variable names.
datetocheck = repmat(TableA.date, 1, height(TableB));  %replicate in as many columns as there are rows in B
datestart = repmat(TableB.datestart', height(TableA), 1); %tranpose and replicate in as many rows as in A
dateend = repmat(TableB.dateend', height(TableA), 1);
toremove = any(datetocheck >= datestart & datetocheck <= dateend, 2); 
TableA(toremove, :) = [];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多