在MATLAB中查找相似的行 [英] Finding similar rows in MATLAB

查看:140
本文介绍了在MATLAB中查找相似的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量行的矩阵.我还有另一个矩阵,可以一次遍历一行.对于第二矩阵中的每一行,我需要在第一矩阵中查找相似的行.找到所有相似的行后,我需要知道相似行的行号.这些行几乎永远不会是精确的,因此ismember不起作用.

I have a matrix with a large number of rows. I have another matrix that I will loop through one row at a time. For each row in the second matrix, I need to look for similar rows in the first matrix. Once all the similar rows are found, I need to know the row numbers of the similar rows. These rows will almost never be exact, so ismember does not work.

此外,该解决方案最好(不一定)提供某种方式来设置相似度,以触发代码说相似度并为我提供行号.

Also, the solution would preferably (not necessarily, however) give some way to set a level of similarity that would trigger the code to say it is similar and give me the row number.

有没有办法做到这一点?我环顾四周,什么也找不到.

Is there any way to do this? I've looked around, and I can't find anything.

推荐答案

您可以使用余弦距离,该距离可以找到两个向量之间的夹角.相似的向量(在您的情况下,是行和比较向量)的值接近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.

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

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