如何衡量两个向量之间的相似性?

时间:2017-09-27 19:47:31

标签: matlab matrix vector

我有两个载体,例如

Aideal =兰特(256,1);

和 A_estimated =兰特(256,1);

如何衡量相似度?通过相似性我的意思是我希望A_estimated的每个元素与Aideal的元素几乎相同。

任何人都可以帮忙。

2 个答案:

答案 0 :(得分:2)

mae(A-B) % mean(abs(A-B)) % Average or mean value of array

sae(A-B) % sum(abs(A-B)) % Sum absolute error performance function

norm(A-B,1) % sum(abs(A-B)) % 1-norm of the vector, which is the sum of the element magnitudes.

norm(A-B,inf) % max(abs(A-B)) % maximum absolute row sum of the diff of vectors.

mse(A-B) % mean((A-B).^2) % Mean of Sum of squared error

sse(A-B) % sum((A-B).^2)  %  Sum of squared error 

norm(A-B) % sqrt(sse(A-B)) 

答案 1 :(得分:1)

如果你想比较两个具有相似余弦相似度的矢量,那么代码就足够了

function [similarity] = CosineSimilarity(x1,x2)
%--------------------------------------------------------------------------
% Syntax:       [similarity] = CosineSimilarity(x1,x2);
% 
% Definition:   Cosine similarity is a measure of similarity between two
%       non-zero vectors of an inner product space that measures 
%       the cosine of the angle between them. The cosine of 0° is 
%       1, and it is less than 1 for any other angle. It is thus a
%       judgment of orientation and not magnitude: two vectors 
%       with the same orientation have a cosine similarity of 1, 
%       two vectors at 90° have a similarity of 0, and two vectors
%       diametrically opposed have a similarity of -1, independent
%       of their magnitude. Cosine similarity is particularly used
%       in positive space, where the outcome is neatly bounded in
%       [0,1]. The name derives from the term "direction cosine":
%       in this case, note that unit vectors are maximally 
%       "similar" if they're parallel and maximally "dissimilar"
%       if they're orthogonal (perpendicular). This is analogous 
%       to the cosine, which is unity (maximum value) when the 
%       segments subtend a zero angle and zero (uncorrelated) 
%       when the segments are perpendicular.[1].
%               
% Inputs:       [x1] is a vector
%               [x2] is a vector
%               
% Outputs:      [similarity] is between 0 and 1
%                             
% Complexity:   No
%
% Dependencies  No dependency.
%               
% Author:       Ugur Ayan, PhD
%               ugur.ayan@ugurayan.com.tr
%               http://www.ugurayan.com.tr
%               
% Date:         May 15, 2016
%
% Refrences     [1] https://en.wikipedia.org/wiki/Cosine_similarity
%--------------------------------------------------------------------------
if ( length (x1) == length(x2) )
    similarity = sum(x1.*x2) / (norm(x1) * norm(x2));
else
   disp('Vectors dimensions does  not match'); 
end
相关问题