-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_vecs.m
60 lines (46 loc) · 1.76 KB
/
match_vecs.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
57
58
59
60
function C_out = match_vecs(C_in, C_ref, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Parse Inputs
if nargin < 2
error('Need at least two inputs!')
end
if nargin > 2
greed_type = varargin{1};
else
greed_type = 'greedy';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Run comparison
C_tmp = C_in;
C_out = zeros(size(C_in));
C_tmp_norms = sqrt(sum(C_tmp.^2, 1));
C_ref_norms = sqrt(sum(C_ref.^2, 1));
if strcmp(greed_type, 'greedy') == 1
for kk = 1:size(C_ref, 2)
comp_vals = abs((C_tmp')*C_ref(:, kk))./(sum(C_ref_norms(kk).^2)*C_tmp_norms');
ix = find(comp_vals == max(comp_vals), 1);
C_out(:, kk) = C_tmp(:, ix);
C_tmp = [C_tmp(:, 1:ix-1), C_tmp(:, ix+1:end)];
C_tmp_norms = [C_tmp_norms(1:ix-1), C_tmp_norms(ix+1:end)];
end
elseif strcmp(greed_type, 'global') == 1
norms_mat = (C_tmp_norms')*C_ref_norms;
C_ref_tmp = C_ref;
ind_tmp = 1:size(C_ref, 2);
ind_ref_tmp = 1:size(C_ref, 2);
for kk = 1:size(C_ref, 2)
comp_vals = abs((C_tmp')*C_ref_tmp./norms_mat);
[ix, jx] = find(comp_vals == max(min(comp_vals)), 1);
C_out(:, ind_ref_tmp(jx)) = C_in(:, ind_tmp(ix));
C_tmp = [C_tmp(:, 1:ix-1), C_tmp(:, ix+1:end)];
ind_tmp = [ind_tmp(:, 1:ix-1), ind_tmp(:, ix+1:end)];
C_ref_tmp = [C_ref_tmp(:, 1:jx-1), C_ref_tmp(:, jx+1:end)];
ind_ref_tmp = [ind_ref_tmp(:, 1:jx-1), ind_ref_tmp(:, jx+1:end)];
norms_mat = norms_mat([1:ix-1, ix+1:end], [1:jx-1, jx+1:end]);
end
else
error('Unknown search type!')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%