欧式空间与流形空间下矩阵的距离度量:MATLAB代码

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, 376383. 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

参考

Ham, Jihun and Daniel D. Lee. “Grassmann discriminant analysis: a unifying view on subspace-based learning.” International Conference on Machine Learning (2008).

你可能感兴趣的:(代码与编程,matlab,矩阵,开发语言)