%% Constructs an epsilon-like graph of the data, constrained so that only % the temporally closest frames belonging to the same subject are selected function W = construct_epsilon_graph(X,epsilon,n_TP,n_Subj) % Computes a subject index vector disp('Computing subject index vector...'); S_idx = []; for i = 1:n_Subj S_idx = [S_idx,i*ones(1,n_TP)]; end disp('Computing distances between data points...'); % Computes the distance between all data points D = distance(X',X'); % Adjacency matrix, initilly filled with zeros W = zeros(size(D)); disp('Filling adjacency matrix...'); j = 1:size(D,1); % For all data points, we will consider the epsilon temporally closest % frames for i = 1:size(D,1) % Takes all distances to the considered data point tmp = D(i,:); % Only keeps the ones of closeby data points temporally (in both % directions) AND of data points belonging to the same subjects tmp = tmp( (abs(j-i)<=epsilon) & (S_idx(i)==S_idx(j)) ); divide_factor = epsilon; % Those have a distance value computed tmp = exp(-tmp.^2/(divide_factor^2)); tmp(isnan(tmp)) = 0; % Adjacency filled accordingly W(i,(abs(j-i)<=epsilon) & (S_idx(i)==S_idx(j))) = tmp; end end function d = distance(a,b) % DISTANCE - computes Euclidean distance matrix % % E = distance(A,B) % % A - (DxM) matrix % B - (DxN) matrix % % Returns: % E - (MxN) Euclidean distances between vectors in A and B % % % Description : % This fully vectorized (VERY FAST!) m-file computes the % Euclidean distance between two vectors by: % % ||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B ) % % Example : % A = rand(400,100); B = rand(400,200); % d = distance(A,B); % Author : Roland Bunschoten % University of Amsterdam % Intelligent Autonomous Systems (IAS) group % Kruislaan 403 1098 SJ Amsterdam % tel.(+31)20-5257524 % bunschot@wins.uva.nl % Last Rev : Oct 29 16:35:48 MET DST 1999 % Tested : PC Matlab v5.2 and Solaris Matlab v5.3 % Thanx : Nikos Vlassis % Copyright notice: You are free to modify, extend and distribute % this code granted that the author of the original code is % mentioned as the original author of the code. if (nargin ~= 2) error('Not enough input arguments'); end if (size(a,1) ~= size(b,1)) error('A and B should be of same dimensionality'); end aa=sum(a.*a,1); bb=sum(b.*b,1); ab=a'*b; d = sqrt(abs(repmat(aa',[1 size(bb,2)]) + repmat(bb,[size(aa,2) 1]) - 2*ab)); end