function distance = MatDistance(Y1, Y2, metric)
% Ref: (of subspace distance)
% Jihun Hamm and Daniel D. Lee. 2008. ...
% Grassmann discriminant analysis: a unifying view on subspace-based learning. ...
% In Proceedings of the 25th international conference on Machine learning (ICML '08). ...
% Association for Computing Machinery, New York, NY, USA, 376–383. https://doi.org/10.1145/1390156.1390204
% download website:
% https://dl.acm.org/doi/pdf/10.1145/1390156.1390204
% ** metric includes: **
% 1. Euclidean Metric
% 2. Projection Metric
% 3. Binet-Cauchy Metric
% 4. Max Correlation Metric
% 5. Min Correlation Metric
% 6. Procrustes Metrc = Chordal Metric
if strcmp(metric, 'Euclidean')
%% Euclidean distance
distance = norm(Y1 - Y2, 'fro');
else
%% Subspace distance
% Validate whether Y1 and Y2 are orthogonal
[D, m ] = size(Y1); % generally, we have D >= m
if ( norm( (Y1' * Y1) - eye(m), 'fro' )^2 / (m * m) < 1e-4 ) && ...
( norm( (Y2' * Y2) - eye(m), 'fro' )^2 / (m * m) < 1e-4 )
[U, S, V] = svd(Y1' * Y2); % S = diag( [cos(theta_1), ... cos(theta_m)] )
cos_list = diag(S);
%% Subspace distance
switch metric
case 'Projection'
distance = sqrt( m - sum(cos_list.^2) );
case 'Binet-Cauchy'
distance = sqrt( 1- prod(cos_list.^2) );
case 'Max Correlation'
distance = sqrt( 1- cos_list(1)^2 );
case 'Min Correlation'
distance = sqrt( 1 - cos_list(m)^2 );
case 'Procrustes' % identical with Chordal distance
distance = norm(Y1 * U - Y2 * V, 'fro');
case 'Chordal'
distance = norm(Y1 * U - Y2 * V, 'fro');
otherwise
%
disp('Wrong: The metric name you input is wrong! Plear correct it.')
end
else
%
disp('Wrong: There may be two problems happened: ')
disp('1-For substace distance, the input Y1 and Y2 should be semi-orthogonal.')
disp('2-The metric name you input is wrong! Plear correct it.')
end
end
end
欧式空间与流形空间下矩阵的距离度量:MATLAB代码
最新推荐文章于 2025-03-27 16:30:45 发布