-
Notifications
You must be signed in to change notification settings - Fork 30
/
filter_unique_rows.m
57 lines (44 loc) · 1.7 KB
/
filter_unique_rows.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%FILTER_UNIQUE_ROWS Filter the rows of a set of matrices using a column
%
% MATRICES = FILTER_UNIQUE_ROWS(MATRICES, KEY_COLUMN)
%
% Filter the MATRICES by comparing the values in each KEY_COLUMN. If one
% of the matrices has a value in the KEY_COLUMN which is unique, that
% corresponding row is left out.
%
% Input arguments:
% - MATRICES: A -cell- structure of matrices. The can be of different
% length and size. All the MATRICES must have a KEY_COLUMN in common.
% - KEY_COLUMN: The column which all MATRICES have in common, used to
% compare values and search for unique rows.
%
% Output values:
% - MATRICES: The -cell- structure with the MATRICES, where in each
% matrix the unique rows, based on KEY_COLUMN, are filtered out.
%
% Example:
% matrices = {accelerometer(:,2:4), magnetic(:,2:4), rotation(:,2:4)};
% filtered = filter_unique_rows(matrices, 1);
% data = [accelerometer(:,2) filtered{1} filtered{2} filtered{3}];
function matrices = filter_unique_rows( matrices, key_column )
if nargin < 2 key_column = 1; end
nr = length(matrices);
removals = cell(nr, nr);
for i = 1 : nr
for j = 1 : nr
if i == j continue; end
set_i = matrices{i};
set_j = matrices{j};
removals{i,j} = find(ismember(set_i(:,key_column), set_j(:, key_column)) == 0);
end
end
for i = 1 : nr
remove_indices = [];
for j = 1 : nr
remove_indices = [remove_indices removals{i,j}'];
end
set = matrices{i};
set(remove_indices, :) = [];
matrices{i} = set;
end
end