matlab - 计算欧几里得距离
问题描述
我有 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 矩阵值
解决方案
更优雅的答案(来自下面的评论):
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 )
推荐阅读
- python - 我可以使用 python 的 np.savez 来保存列表列表吗?
- c# - 将新项目添加到 ObservableCollection 时,UI 仅更新一次
- monitoring - 如何在将 Telegraf 中的指标发送到 Datadog 之前对其进行过滤?
- xamarin.forms - 如何在 CollectionView 中绑定 Picker?
- python-3.x - 向 flask-restplus 发送多个参数
- python - 您如何像 Mathematica 那样执行这种不正确的积分?
- c# - 将 JSON 反序列化为 C# 对象列表
- android - Google App Store:如何测试订阅?
- ruby-on-rails - 渲染静态页面时 Rails 路由冲突
- python - 为什么 [None] * 10 比 [None for i in range(10)] 快