如何在 MATLAB 中创建相似矩阵?
我正在努力比较多个图像。我将这些图像数据作为称为“图像”的矩阵的列向量。我想通过首先计算图像的欧几里得距离来评估图像的相似性。然后我想创建一个可以执行多次随机游走的矩阵。现在,我的代码如下:
% clear
% clc
% close all
%
% load tea.mat;
images = Input.X;
M = zeros(size(images, 2), size (images, 2));
for i = 1:size(images, 2)
for j = 1:size(images, 2)
normImageTemp = sqrt((sum((images(:, i) - images(:, j))./256).^2));
%Need to accurately select the value of gamma_i
gamma_i = 1/10;
M(i, j) = exp(-gamma_i.*normImageTemp);
end
end
然而,我的矩阵 M 最终沿其主对角线的值为 1,其他地方的值为 0。我期望每行的前几个元素具有“大”值,并且列索引 > 的元素具有“小”值。 4. 有人可以解释一下出了什么问题吗?任何建议表示赞赏。
I am working towards comparing multiple images. I have these image data as column vectors of a matrix called "images." I want to assess the similarity of images by first computing their Eucledian distance. I then want to create a matrix over which I can execute multiple random walks. Right now, my code is as follows:
% clear
% clc
% close all
%
% load tea.mat;
images = Input.X;
M = zeros(size(images, 2), size (images, 2));
for i = 1:size(images, 2)
for j = 1:size(images, 2)
normImageTemp = sqrt((sum((images(:, i) - images(:, j))./256).^2));
%Need to accurately select the value of gamma_i
gamma_i = 1/10;
M(i, j) = exp(-gamma_i.*normImageTemp);
end
end
My matrix M however, ends up having a value of 1 along its main diagonal and zeros elsewhere. I'm expecting "large" values for the first few elements of each row and "small" values for elements with column index > 4. Could someone please explain what is wrong? Any advice is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您正在尝试计算欧几里得距离,因此您的位置似乎存在错误计算
normImageTemp
时会放置括号。你有这个:但你实际上想要这样做:
换句话说,你需要执行按元素求平方,然后求和,然后求平方根。您现在要做的是首先对元素求和,然后对元素进行平方并取总和的平方根,这本质上是相互抵消的(或者实际上相当于只取绝对值)。
顺便说一句,您实际上可以使用函数 NORM 来为您执行此操作,像这样:
Since you're trying to compute a Euclidean distance, it looks like you have an error in where your parentheses are placed when you compute
normImageTemp
. You have this:But you actually want to do this:
In other words, you need to perform the element-wise squaring, then the summation, then the square root. What you are doing now is summing elements first, then squaring and taking the square root of the summation, which essentially cancel each other out (or are actually the equivalent of just taking the absolute value).
Incidentally, you can actually use the function NORM to perform this operation for you, like so:
您得到的结果似乎是合理的。回想一下 exp(-x) 的行为。当 x 为零时,exp(-x) 为 1。当 x 较大时,exp(-x) 为零。
也许如果你使 M(i,j) =normImageTemp;你会看到你期望看到的。
The results you're getting seem reasonable. Recall the behavior of the exp(-x). When x is zero, exp(-x) is 1. When x is large exp(-x) is zero.
Perhaps if you make M(i,j) = normImageTemp; you'd see what you expect to see.
考虑这个解决方案:
PDIST 和 SQUAREFORM 是统计工具箱中的函数。
否则考虑这个等效的矢量化代码(仅使用内置函数):
正如其他答案中所解释的,
D
是距离矩阵,而exp(-D)
是相似度矩阵(这就是为什么你会在对角线上得到相似度矩阵)Consider this solution:
PDIST and SQUAREFORM are functions from the Statistics Toolbox.
Otherwise consider this equivalent vectorized code (using only built-in functions):
As was explained in the other answers,
D
is the distance matrix, whileexp(-D)
is the similarity matrix (which is why you get ones on the diagonal)有一个已经实现的函数pdist,如果你有一个矩阵A,你可以直接做
Sim= squareform(pdist(A))
there is an already implemented function pdist, if you have a matrix A, you can directly do
Sim= squareform(pdist(A))