我有两个表(或实际上我有几个)应该使用innerjoin
合并。它按预期工作;除了它按“键”排序,它会破坏我的数据,因为它实际上必须按原始行顺序排列。
从帮助部分:
中的值排序
C
按关键变量
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
?
答案 0 :(得分:2)
除了结果表之外,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