马氏距离

本文介绍了两种计算Mahalanobis距离的方法:一种是通过自定义函数mahaldist,另一种是利用统计工具箱中的函数mahaldist_stats。这两种方法都适用于计算矩阵X中的每个向量与矩阵Y中所有向量之间的Mahalanobis距离。
mahaldist.m
%mahaldist: Mahalanobis distance between X(i, :) and Y(j, :)
%
%  [d] = mahaldist(X, Y)
%
%  Input and output arguments ([]'s are optional):
%   X     (matrix) of size NxD. N is the number of vectors, 
%           and D is the dimension of each vector. 
%   Y     (matrix) of size PxD. P is the number of vectors,
%           and D is the dimension of each vector. 
%   d     (matrix) of size NxP. d(i, j) is the mahalanobis distance
%           between X(i, :) and Y(j, :). 
%
% Reference: pdist of statistics toolbox
%
% Author : Naotoshi Seo
% Date   : June, 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function d = mahaldist(X, Y)
 [N D] = size(X);
 [P D] = size(Y);
 A = [X; Y];   % pdist was doing as 2nd way, but 1st way was faster. 
 invcov = inv(cov(A));
 % invcov = cov(A) / eye(D);   for i=1:N
    % Compute distances from one vector toward vectors at burst. 
    diff = repmat(X(i, :), P, 1) - Y;
    %dsq(i, :) = diag(diff * invcov * diff'); % only diag is necessary, burdernsome
    dsq(i, :) = sum((diff*invcov).*diff , 2);
 end
 d = sqrt(dsq);
mahaldist_stats.m
%mahaldist_stats: Mahalanobis distance between X(i, :) and Y(j, :) using
%  statistics toolbox
%
%  [d] = mahaldist_stats(X, Y)
%
%  Input and output arguments ([]'s are optional):
%   X     (matrix) of size NxD. N is the number of vectors, 
%           and D is the dimension of each vector. 
%   Y     (matrix) of size PxD. P is the number of vectors,
%           and D is the dimension of each vector. 
%   d     (matrix) of size NxP. d(i, j) is the mahalanobis distance
%           between X(i, :) and Y(j, :). 
%
% Requirement: statistics toolbox
%
% Author : Naotoshi Seo
% Date   : June, 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function d = mahaldist_stats(X, Y)
 [N D] = size(X);
 [P D] = size(Y);
 A = [X; Y]; 
 % calculation of distance between X(i, :) and X(j, :) is wortheless, though
 d = squareform(pdist(A, 'mahalanobis'));
 d = d(1:N, N+1:end);
mahaldistTest.m
function mahaldistTest
 proto = [
    0.6213    0.7373
    0.5226    0.8939
    0.9797    0.6614
    0.9568    0.0118
    0.8801    0.1991
    0.8757    0.0648
    0.1730    0.2987
    0.2714    0.2844
    0.2523    0.4692
    ];
 data = [
    0.9883    0.4329
    0.5828    0.2259
    0.4235    0.5798
    0.5155    0.7604
    0.3340    0.5298
    ];
 d = mahaldist(proto, data)
 d_stats = mahaldist_stats(proto, data)   X = rand(200, 100);
 Y = rand(200, 100);
 tic
 mahaldist(X, Y);
 toc
 tic
 mahaldist_stats(X, Y);
 toc
d =

    1.5219    1.9870    1.0235    0.3631    1.4269
    2.1044    2.5058    1.3251    0.5157    1.6762
    0.8648    2.4137    2.0606    1.5932    2.4486
    1.6359    1.3790    2.5120    2.9112    2.5857
    1.0500    1.0308    1.8977    2.2219    2.0397
    1.5470    1.0710    2.2191    2.6570    2.2805
    3.0419    1.4079    1.5431    2.3606    1.1565
    2.7225    1.0694    1.3580    2.1863    1.0113
    2.5706    1.3082    0.8151    1.6082    0.4102


d_stats =

    1.5219    1.9870    1.0235    0.3631    1.4269
    2.1044    2.5058    1.3251    0.5157    1.6762
    0.8648    2.4137    2.0606    1.5932    2.4486
    1.6359    1.3790    2.5120    2.9112    2.5857
    1.0500    1.0308    1.8977    2.2219    2.0397
    1.5470    1.0710    2.2191    2.6570    2.2805
    3.0419    1.4079    1.5431    2.3606    1.1565
    2.7225    1.0694    1.3580    2.1863    1.0113
    2.5706    1.3082    0.8151    1.6082    0.4102
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值