首页 > 解决方案 > 计算欧几里得距离

问题描述

我有 200 张面部图像(32x32),我将其重塑为矩阵数据

人脸 = 200 x 1024(200 是人脸样本,1024 是一张人脸图像矢量化为 1 行)

Facemean = 1 x 1024(图像的平均值)

欧几里得距离方程 1 的使用将是:

 distance = sqrt ( (Face(1,1)-Facemean(1,1)^2 + (Face(1,2)-Facemean(1,2)^2  + . . . + (Face(200,1024)-Facemean(1,1024)^2 );

由于使用上面的公式会很长,所以我想问一下是否有其他方法可以计算它?

我期望的结果是 1 x 1 矩阵值

标签: matlab

解决方案


更优雅的答案(来自下面的评论):

FacemeanMatrix = repmat( Facemean, [200, 1])
distance = sqrt( sum( ( Face(:)-FacemeanMatrix(:) ).^2 )) 

上一个答案

你可以把它写成向量:

dissqr = 0
% for each colum, calculate the square difference and sum all the lines
for i =1:200
  dissqr = dissqr + sum( (Face(i,:)-(Facemean(:,i).'))^2 )
end

distance = sqrt( dissqr )

推荐阅读