matlab - 合并表而不按键排序

标签 matlab sorting inner-join

我有两个表(或者实际上我有几个),应该使用 innerjoin 合并它们。它按预期工作;除了它按“键”排序,这会破坏我的数据,因为它实际上必须按原始行顺序排列。

来自帮助部分:

C is sorted by the values in the key variables

t1 = Case Val  
3 1  
1 1  
2 1  

t2 = Val2 Case Subset  
2 2 2  
1 2 2  
2 3 1  

tnew = innerjoin(t1,t2)

tnew = Case Val Val2 Subset  
2 ...   
% will start with "2" since its a lower value than "3", but in t1 "3" was in lower row than "2", it is rearranged

我应该如何避免排序?无法使用innerjoin

最佳答案

除了结果表之外,innerjoin 还返回两个额外的输出:第一个表的行索引和第二个表的与输出中的行相对应的行索引。

您可以简单地使用第二个输出来确定 t1 中使用的行,并且可以对这些行进行排序。然后使用排序顺序更改连接结果中行的顺序。

%// Setup the data
t1 = table([3;1;2], [1;1;1], 'VariableNames', {'Case', 'Val'});
t2 = table([2;1;2],[2;2;3],[2;2;1], 'VariableNames', {'Val2', 'Case', 'Subset'});


%// Perform the inner join and keep track of where the rows were in t1
[tnew, rows_in_t1] = innerjoin(t1, t2);

%// Sort them in order to maintain the order in t1
[~, sortinds] = sort(rows_in_t1);

%// Apply this sort order to the new table
tnew = tnew(sortinds,:);

%// Case    Val    Val2    Subset
%// ____    ___    ____    ______
%// 3       1      2       1     
%// 2       1      2       2     
%// 2       1      1       2  

关于matlab - 合并表而不按键排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850027/

相关文章:

arrays - MATLAB 2013a : sum + squeeze dimension inconsistencies

java - 如何对扩展类的ArrayList进行排序?

php - 如何对内部联接进行联接?

php - MySQL 查询为我正在制作的论坛创建 "bump-system"

Java - 与 3 个表的内连接

当股息提高到一个幂时,matlab 错误的模数结果

matlab - 如何在Matlab中构建停止声音功能?

matlab - 如何在 MATLAB 中生成二维随机向量?

php - PHP 的 usort 适用于哪些排序算法?

algorithm - 在 O(n) 中运行的数组 "maximum difference"算法?