如何在MATLAB中创建相似矩阵? [英] How do I create a simliarity matrix in MATLAB?

查看:506
本文介绍了如何在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,而其他地方为零.我期望每行的前几个元素的大"值和列索引> 4的元素的小"值.请问有人可以解释出什么问题吗?任何建议表示赞赏.

解决方案

由于您正在尝试计算欧几里得距离,当您计算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);

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.

解决方案

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);

这篇关于如何在MATLAB中创建相似矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆