且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在MATLAB中查找相似的行

更新时间:2021-10-14 22:41:11

您可以使用余弦距离,该距离可以找到两个向量之间的夹角.相似的向量(在您的情况下,是行和比较向量)的值接近1,不相似的向量的值接近0.

You could use cosine distance, which finds the angle between two vectors. Similar vectors (in your case, a row and your comparison vector) have a value close to 1 and dissimilar vectors have a value close to 0.

function d = cosSimilarity(u, v)
  d = dot(u,v)/(norm(u)*norm(v));
end

要将此函数应用于矩阵MV中的所有对行,可以使用嵌套的for循环.几乎不是最优雅的,但是它可以工作:

To apply this function to each to all pairs of rows in the matrices M and V you could use nested for loops. Hardly the most elegant, but it will work:

numRowsM = size(M, 1)
numRowsV = size(V, 1)
similarThresh = .9

for m = 1:numRowsM
    for v = 1:numRowsV 
        similarity = cosSimilarity(V(v,:), M(m, :))

        % Notify about similar rows
        if similarity > similarThresh
            disp([num2str(m) ' is similar to a row in V'])
        end
    end
end

除了嵌套的for循环外,肯定还有其他方法.您可以从此中查看解决方案开始. a>问题,它将矩阵的行转换为单元格数组的单元格,然后使用cellfun应用该函数,从而有助于避免循环.

Instead of nested for loops, there are definitely other ways. You could start by looking at the solution from this question, which will help you avoid the loop by converting the rows of the matrix into cells of a cell array and then applying the function with cellfun.