在两个数组的MATLAB中,用相同的索引计算相似值的个数。

时间:2022-06-23 12:47:58

I am trying to count the number of similar elements in two arrays, which can be done by the intersect function but I need to get only the values which are similar and have the same index. Any ideas?

我正在尝试计算两个数组中相似元素的数量,这可以由intersect函数完成,但我只需要获得相似且具有相同索引的值。什么好主意吗?

1 个解决方案

#1


4  

If you are looking for how many entries in the two matrices are "nearly" the same, then set some tolerance tol, and then you want to find how many corresponding entries in your matrices (call them A and B) differ by less than tol.

如果你在寻找两个矩阵中有多少个元素是“接近”相同的,然后设置一些容忍醇,然后你想要找到你的矩阵中有多少对应的元素(称为A和B)的差异小于tol。

abs(A-B)<tol

is a matrix the same size as A and B which has a 1 where the elements are close together, and a 0 where they're not. You can use

是一个和a和B大小相同的矩阵它有1个元素在一起,而0个元素不在。您可以使用

[i,j]=find(abs(A-B)<tol)

to get the positions of the nearly-mathcing elements, or

获取近似计算元素的位置

nnz(abs(A-B)<tol)

to just count how many values nearly-match.

只要数一下有多少值接近匹配。

#1


4  

If you are looking for how many entries in the two matrices are "nearly" the same, then set some tolerance tol, and then you want to find how many corresponding entries in your matrices (call them A and B) differ by less than tol.

如果你在寻找两个矩阵中有多少个元素是“接近”相同的,然后设置一些容忍醇,然后你想要找到你的矩阵中有多少对应的元素(称为A和B)的差异小于tol。

abs(A-B)<tol

is a matrix the same size as A and B which has a 1 where the elements are close together, and a 0 where they're not. You can use

是一个和a和B大小相同的矩阵它有1个元素在一起,而0个元素不在。您可以使用

[i,j]=find(abs(A-B)<tol)

to get the positions of the nearly-mathcing elements, or

获取近似计算元素的位置

nnz(abs(A-B)<tol)

to just count how many values nearly-match.

只要数一下有多少值接近匹配。