更快的方式,采取一个行和1的矩阵和0之间的异或? [英] Faster way to take xor between one row and a matrix of 1's and 0's?

查看:250
本文介绍了更快的方式,采取一个行和1的矩阵和0之间的异或?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一行数据,比如 A = [0 1 1 1 0 0]

矩阵B包含许多行。对于一个虚拟的例子让我们说这只是 B = [1 1 1 0 1 0; 1 0 0 1 0 1]

Matrix B contains many rows. For a dummy example let's say it's just B = [1 1 1 0 1 0; 1 0 0 1 0 1].

我想找到,其中A和B的行不同的列数,并且使用差异该矢量找到其中B的行最相似A.因此,对于上面的例子中,A从<$不同C $ C> B(1:)中,列1 4,5 = 3的总差别。从B(2一个是不同的,在:),列1 2,3,6 = 4的总差别,所以我要回报指数1,表明A是最相似的B(1:)

I want to find the number of columns in which A and a row of B differ, and use that vector of differences to find which row of B is most similar to A. So for the example above, A differs from B(1,:) in columns 1, 4, 5 = 3 total difference. A differs from B(2,:) in columns 1, 2, 3, 6 = 4 total differences, and so I would want to return index 1 to indicate that A is most similar to B(1,:).

在现实中B有〜50,000行后,A和B都对 800列即可。我现在的code找到最相似的行如下:

In reality B has ~50,000 rows, and A and B both have about 800 columns. My current code to find the most similar row is below:

min(sum(xor(repmat(A,B_rows,1),B),2));

这样的作品,但它是非常缓慢的。任何见解哪个函数这么长时间以及如何改进呢?

That works, but it's very slow. Any insights into which function is taking so long and ways to improve it?

推荐答案

有6或多或少的相似与 bsxfun 的办法,很难说哪一个是最快的。

There are 6 more or less similar approaches with bsxfun, hard to tell which one is the fastest.

%example data
A = [0 1 1 1 0 0];
B = perms(A);     %720x6 matrix

计数的相似之处:

Counting similarities:

Z = sum(  bsxfun(@eq,  A,B) , 2);
Z = sum( ~bsxfun(@ne,  A,B) , 2);
Z = sum( ~bsxfun(@xor, A,B) , 2);

计数差异:

Z = sum( ~bsxfun(@eq,  A,B) , 2);
Z = sum(  bsxfun(@ne,  A,B) , 2);
Z = sum(  bsxfun(@xor, A,B) , 2);

以Z 是包含 A 的平等/不平等的元素个数为<$ C的每一行向量$ C> B 。

Z is a vector containing the number of equal/unequal elements of A for every row of B.

基准100试验的每个(与上述顺序相同):

Benchmark for 100 trials each (same order as above):

t100 =

    0.0081
    0.0098
    0.0123
    0.0102
    0.0087
    0.0111

这篇关于更快的方式,采取一个行和1的矩阵和0之间的异或?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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