1 | function [iFound, iNotFound, iFoundList1] = findrowindex(List1, List2) |
---|
2 | %FINDROWINDEX |
---|
3 | % [iFound, iNotFound, iFoundList1] = findrowindex(List1, List2) |
---|
4 | % |
---|
5 | % INPUTS |
---|
6 | % 1. List1 and List2 are matrices with the same number of columns. |
---|
7 | % This function return the row indicies where each row of List1 |
---|
8 | % is found in List2. |
---|
9 | % |
---|
10 | % OUTPUTS |
---|
11 | % 1. iFound are the row numbers of List2 where a row of List1 was found. |
---|
12 | % If there are multiple matches, then only the smallest index is used. |
---|
13 | % 2. iNotFound are the row number of List1 which was not found in List2. |
---|
14 | % 3. iFoundList1 are the indicies of List1 where List1 rows were found in List2 |
---|
15 | % length(iFoundList1) always equals the length(iFound), iFoundList1 just |
---|
16 | % references List1. |
---|
17 | % |
---|
18 | % If all the rows of List1 are also in List2, then |
---|
19 | % List1 = List2(iFound,:) |
---|
20 | % If not, |
---|
21 | % List1(iFoundList1,:) = List2(iFound,:) |
---|
22 | % |
---|
23 | % Written by Greg Portmann |
---|
24 | |
---|
25 | iFound = []; |
---|
26 | iNotFound = []; |
---|
27 | iFoundList1 = []; |
---|
28 | |
---|
29 | if isempty(List1) | isempty(List2) |
---|
30 | return |
---|
31 | end |
---|
32 | |
---|
33 | N = size(List1,2); |
---|
34 | |
---|
35 | if N ~= size(List2,2) |
---|
36 | error('List1 and List2 must have the same number of columns'); |
---|
37 | end |
---|
38 | |
---|
39 | for i = 1:size(List1,1) |
---|
40 | % Make N = 1 or 2 a special case for speed reasons (this may not be true or required) |
---|
41 | if N == 1 |
---|
42 | k = find(List1(i)==List2); |
---|
43 | elseif N == 2 |
---|
44 | k = find(List1(i,1)==List2(:,1) & List1(i,2)==List2(:,2)); |
---|
45 | else |
---|
46 | k = (List1(i,1)==List2(:,1)); |
---|
47 | for j = 2:N |
---|
48 | k = k & (List1(i,j)==List2(:,j)); |
---|
49 | end |
---|
50 | k = find(k); |
---|
51 | end |
---|
52 | |
---|
53 | if isempty(k) |
---|
54 | iNotFound = [iNotFound; i]; |
---|
55 | else |
---|
56 | iFound = [iFound; k(1)]; |
---|
57 | iFoundList1 = [iFoundList1; i]; |
---|
58 | end |
---|
59 | end |
---|
60 | |
---|
61 | |
---|
62 | % member = ismember(List2,List1,'rows'); |
---|
63 | % %DeviceListTotal(member,:); |
---|
64 | % iFound = find(member); |
---|
65 | % iNotFound = []; |
---|
66 | |
---|
67 | |
---|
68 | % The unique and intersect method seems to be a little slower |
---|
69 | % |
---|
70 | % % Intersect removes duplicate devices so first store index of how to unsort in j_unique |
---|
71 | % DeviceListOld = List1; |
---|
72 | % [List1, i_unique, j_unique] = unique(List1, 'rows'); |
---|
73 | % |
---|
74 | % % Find the indexes in the full device list (reorder and remove duplicates) |
---|
75 | % [List1, ii, DeviceIndex] = intersect(List1, List2, 'rows'); |
---|
76 | % if size(List1,1) ~= size(DeviceListOld,1) |
---|
77 | % % All devices must exist (duplicate devices ok) |
---|
78 | % [DeviceListNotFound, iNotFound] = setdiff(DeviceListOld, List2, 'rows'); |
---|
79 | % %if length(iNotFound) > 0 |
---|
80 | % % % Device not found |
---|
81 | % % for i = 1:length(iNotFound) |
---|
82 | % % fprintf(' No devices to get for (%d,%d)\n', DeviceListNotFound(i,1), DeviceListNotFound(i,2)); |
---|
83 | % % end |
---|
84 | % % error(sprintf('%d Devices not found', length(iNotFound))); |
---|
85 | % %end |
---|
86 | % end |
---|
87 | % iFound = DeviceIndex(j_unique); % Reorder and add multiple elements back |
---|
88 | |
---|