1 | function [c,a_match,b_match] = union_sorted_rows(a,b)
|
---|
2 | %UNION_SORTED_ROWS Set union of sorted sets of row vectors.
|
---|
3 | % UNION_SORTED_ROWS(A,B) where A and B are matrices returns the combined rows
|
---|
4 | % from A and B with no repetitions. Rows of A (and B) must be sorted and
|
---|
5 | % unique, and the resulting rows will be sorted and unique.
|
---|
6 | %
|
---|
7 | % [C,A_MATCH,B_MATCH] = UNION_SORTED_ROWS(A,B) also returns
|
---|
8 | % A_MATCH = MATCH_SORTED_ROWS(A,C)
|
---|
9 | % B_MATCH = MATCH_SORTED_ROWS(B,C)
|
---|
10 | %
|
---|
11 | % Examples:
|
---|
12 | % union_sorted_rows([10 20; 30 40], [30 40; 50 60])
|
---|
13 | % [c,a_match,b_match] = union_sorted_rows([10 20; 30 40], [30 40; 50 60])
|
---|
14 |
|
---|
15 | % Written by Tom Minka
|
---|
16 | % (c) Microsoft Corporation. All rights reserved.
|
---|
17 |
|
---|
18 | if nargout <= 1
|
---|
19 | c = sortrows([a(~ismember_sorted_rows(a,b),:); b]);
|
---|
20 | else
|
---|
21 | [tf,loc] = ismember_sorted_rows(a,b);
|
---|
22 | [c,i] = sortrows([a(~tf,:); b]);
|
---|
23 | nc = rows(c);
|
---|
24 | nb = rows(b);
|
---|
25 | na = rows(a);
|
---|
26 | b_match = i((nc-nb+1):nc);
|
---|
27 | a_match = zeros(1,na);
|
---|
28 | a_match(~tf) = i(1:(nc-nb));
|
---|
29 | a_match(tf) = b_match(loc(tf));
|
---|
30 | end
|
---|