-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLC_coding_appr.m
52 lines (46 loc) · 1.52 KB
/
LLC_coding_appr.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
% ========================================================================
% USAGE: [Coeff]=LLC_coding_appr(B,X,knn,lambda)
% Approximated Locality-constraint Linear Coding
%
% Inputs
% B -M x d codebook, M entries in a d-dim space
% X -N x d matrix, N data points in a d-dim space
% knn -number of nearest neighboring
% lambda -regulerization to improve condition
%
% Outputs
% Coeff -N x M matrix, each row is a code for corresponding X
%
% Jinjun Wang, march 19, 2010
% ========================================================================
function [Coeff] = LLC_coding_appr(B, X, knn, beta)
if ~exist('knn', 'var') || isempty(knn),
knn = 5;
end
if ~exist('beta', 'var') || isempty(beta),
beta = 1e-4;
end
nframe=size(X,1);
nbase=size(B,1);
% find k nearest neighbors
XX = sum(X.*X, 2);
BB = sum(B.*B, 2);
D = repmat(XX, 1, nbase)-2*X*B'+repmat(BB', nframe, 1);
IDX = zeros(nframe, knn);
for i = 1:nframe,
d = D(i,:);
[dummy, idx] = sort(d, 'ascend');
IDX(i, :) = idx(1:knn);
end
% llc approximation coding
II = eye(knn, knn);
Coeff = zeros(nframe, nbase);
for i=1:nframe
idx = IDX(i,:);
z = B(idx,:) - repmat(X(i,:), knn, 1); % shift ith pt to origin
C = z*z'; % local covariance
C = C + II*beta*trace(C); % regularlization (K>D)
w = C\ones(knn,1);
w = w/sum(w); % enforce sum(w)=1
Coeff(i,idx) = w';
end