matlab - matlab : pdist2() vs. mahal()函数中的马氏距离

标签 matlab

我有两个矩阵 X 和 Y。它们都代表 3D 空间中的多个位置。 X是50*3的矩阵,Y是60*3的矩阵。

我的问题:为什么将均值函数应用于 pdist2() 的输出并结合“Mahalanobis”不能给出使用 mahal() 获得的结果?

关于我在下面尝试做的事情的更多细节,以及我用来测试它的代码。

假设矩阵 Y 中的 60 个观测值是在某种实验操作之后获得的。我正在尝试评估这种操作是否对 Y 中观察到的位置产生重大影响。因此,我使用 pdist2(X,X,'Mahalanobis') 将 X 与 X 进行比较以获得基线,然后是 X 到 Y(X 是引用矩阵:pdist2(X,Y,'Mahalanobis')),我绘制了两个分布以查看重叠。

随后,我计算了两个分布的平均马氏距离和 95% 置信区间,并进行了 t 检验和 Kolmogorov-Smirnoff 检验以评估分布之间的差异是否显着。这对我来说似乎非常直观,但是,当使用 mahal() 进行测试时,我得到了不同的值,尽管引用矩阵是相同的。我不明白这两种计算马哈拉诺比斯距离的方法之间到底有什么区别。

@3lectrologos 的评论太长了: 你的意思是:d(I) = (Y(I,:)-mu)inv(SIGMA)(Y(I,:)-mu)'?这只是计算 mahalanobis 的公式,因此对于 pdist2() 和 mahal() 函数应该是相同的。我认为 mu 是标量,SIGMA 是基于 pdist2() 和 mahal() 中整体引用分布的矩阵。仅在 mahal 中,您将样本集的每个点与引用分布的点进行比较,而在 pdist2 中,您是根据引用分布进行成对比较。实际上,出于我的目的,我认为我应该使用 mahal() 而不是 pdist2()。我可以根据引用分布解释成对距离,但我认为这不是我在这里需要的。

% test pdist2 vs. mahal in matlab

% the purpose of this script is to see whether the average over the rows of E equals the values in d...

% data
X = []; % 50*3 matrix, data omitted
Y = []; % 60*3 matrix, data omitted


% calculations
S = nancov(X);

% mahal()
d = mahal(Y,X); % gives an 60*1 matrix with a value for each Cartesian element in Y (second matrix is always the reference matrix)

% pairwise mahalanobis distance with pdist2()
E = pdist2(X,Y,'mahalanobis',S); % outputs an 50*60 matrix with each ij-th element the pairwise distance between element X(i,:) and Y(j,:) based on the covariance matrix of X: nancov(X)
%{
 so this is harder to interpret than mahal(), as elements of Y are not just compared to the "mahalanobis-centroid" based on X,
% but to each individual element of X
% so the purpose of this script is to see whether the average over the rows of E equals the values in d...
%}

F = mean(E); % now I averaged over the rows, which means, over all values of X, the reference matrix

mean(d)
mean(E(:)) % not equal to mean(d)
d-F' % not zero

% plot output
figure(1)
plot(d,'bo'), hold on
plot(mean(E),'ro')
legend('mahal()','avaraged over all x values pdist2()')
ylabel('Mahalanobis distance')

figure(2)
plot(d,'bo'), hold on
plot(E','ro')
plot(d,'bo','MarkerFaceColor','b')
xlabel('values in matrix Y (Yi) ... or ... pairwise comparison Yi. (Yi vs. all Xi values)')
ylabel('Mahalanobis distance')
legend('mahal()','pdist2()')

最佳答案

两者之间的一个直接区别是 mahal在计算距离之前从 Y 中的每个点减去 X 的样本均值。

尝试类似 E = pdist2(X,Y-mean(X),'mahalanobis',S); 的操作,看看它是否会为您提供与 mahal 相同的结果>.

关于matlab - matlab : pdist2() vs. mahal()函数中的马氏距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19933883/

相关文章:

matlab - 如何在MATLAB中将逻辑稀疏矩阵转换为整数稀疏矩阵?

image - Matlab:极坐标灰度图

image - 在 Matlab 中将 RGB 映射到 Parula

unit-testing - Matlab xUnit 框架测试套件设置

matlab - 从 Mathematica 调用 Matlab 的非 Windows 方式

MatLab latex 标题不适用于幂 (^)

MySQL 和 MATLAB 64 位

matlab - 将毫秒转换为小时并绘制

matlab - 如何在 MATLAB 中获取 contour3 绘制的曲线句柄?

matlab - 如何使用 CUDA 功能阻止 Matlab 在(错误的)mex 文件执行时崩溃