如何在 MATLAB 中创建相似矩阵?

发布于 2024-10-02 09:41:21 字数 626 浏览 1 评论 0原文

我正在努力比较多个图像。我将这些图像数据作为称为“图像”的矩阵的列向量。我想通过首先计算图像的欧几里得距离来评估图像的相似性。然后我想创建一个可以执行多次随机游走的矩阵。现在,我的代码如下:

% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぽ尐不点ル 2024-10-09 09:41:21

由于您正在尝试计算欧几里得距离,因此您的位置似乎存在错误计算 normImageTemp 时会放置括号。你有这个:

normImageTemp = sqrt((sum((...)./256).^2));
                  %# ^--- Note that this parenthesis...

但你实际上想要这样做:

normImageTemp = sqrt(sum(((...)./256).^2));
                  %#    ^--- ...should be here

换句话说,你需要执行按元素求平方,然后求和,然后求平方根。您现在要做的是首先对元素求和,然后对元素进行平方并取总和的平方根,这本质上是相互抵消的(或者实际上相当于只取绝对值)。

顺便说一句,您实际上可以使用函数 NORM 来为您执行此操作,像这样:

normImageTemp = norm((images(:, i) - images(:, j))./256);

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:

normImageTemp = sqrt((sum((...)./256).^2));
                  %# ^--- Note that this parenthesis...

But you actually want to do this:

normImageTemp = sqrt(sum(((...)./256).^2));
                  %#    ^--- ...should be here

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:

normImageTemp = norm((images(:, i) - images(:, j))./256);
生生不灭 2024-10-09 09:41:21

您得到的结果似乎是合理的。回想一下 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.

何必那么矫情 2024-10-09 09:41:21

考虑这个解决方案:

I = Input.X;

D = squareform( pdist(I') );       %'# euclidean distance between columns of I
M = exp(-(1/10) * D);              %# similarity matrix between columns of I

PDIST 和 SQUAREFORM 是统计工具箱中的函数。

否则考虑这个等效的矢量化代码(仅使用内置函数):

%# we know that: ||u-v||^2 = ||u||^2 + ||v||^2 - 2*u.v
X = sum(I.^2,1);
D = real( sqrt(bsxfun(@plus,X,X')-2*(I'*I)) );
M = exp(-(1/10) * D);

正如其他答案中所解释的,D是距离矩阵,而exp(-D)是相似度矩阵(这就是为什么你会在对角线上得到相似度矩阵)

Consider this solution:

I = Input.X;

D = squareform( pdist(I') );       %'# euclidean distance between columns of I
M = exp(-(1/10) * D);              %# similarity matrix between columns of I

PDIST and SQUAREFORM are functions from the Statistics Toolbox.

Otherwise consider this equivalent vectorized code (using only built-in functions):

%# we know that: ||u-v||^2 = ||u||^2 + ||v||^2 - 2*u.v
X = sum(I.^2,1);
D = real( sqrt(bsxfun(@plus,X,X')-2*(I'*I)) );
M = exp(-(1/10) * D);

As was explained in the other answers, D is the distance matrix, while exp(-D) is the similarity matrix (which is why you get ones on the diagonal)

╰ゝ天使的微笑 2024-10-09 09:41:21

有一个已经实现的函数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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文