检查矩阵行是否等于R中的向量,进行向量化

时间:2021-08-02 16:20:05

I'm very surprised this question has not been asked, maybe the answer will clear up why. I want to compare rows of a matrix to a vector and return whether the row == the vector everywhere. See the example below. I want a vectorized solution, no apply functions because the matrix is too large for slow looping. Suppose there are many rows as well, so I would like to avoid repping the vector.

我很惊讶这个问题没有被问到,也许答案会清楚为什么。我想比较一个矩阵的行和一个向量,并返回行==矢量到处。请参阅下面的示例。我想要一个矢量化解决方案,没有应用函数,因为矩阵对于慢循环来说太大了。假设还有很多行,所以我想避免重复使用向量。

set.seed(1)

M = matrix(rpois(50,5),5,10)

v = c(3 ,   2 ,   7  ,  7 ,   4   , 4   , 7  ,  4  ,  5, 6)

M
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    4    8    3    5    9    4    5    6    7     7
[2,]    4    9    3    6    3    1    5    7    6     1
[3,]    5    6    6   11    6    4    5    2    7     5
[4,]    8    6    4    4    3    8    3    6    5     6
[5,]    3    2    7    7    4    4    7    4    5     6

Output should be

输出应该是

FALSE  FALSE FALSE FALSE TRUE

3 个解决方案

#1


7  

One possibility is

一种可能性是

rowSums(M == v[col(M)]) == ncol(M)
## [1] FALSE FALSE FALSE FALSE  TRUE

Or simlarly

rowSums(M == rep(v, each = nrow(M))) == ncol(M)
## [1] FALSE FALSE FALSE FALSE  TRUE

Or

colSums(t(M) == v) == ncol(M)
## [1] FALSE FALSE FALSE FALSE  TRUE

v[col(M)] is just a shorter version of rep(v, each = nrow(M)) which creates a vector the same size as M (matrix is just a vector, try c(M)) and then compares each element against its corresponding one using ==. Fortunately == is a generic function which has an array method (see methods("Ops") and is.array(M)) which allows us to run rowSums (or colSums) on it in order to makes sure we have the amount of matches as ncol(M)

v [col(M)]只是rep(v,each = nrow(M))的较短版本,它创建一个与M大小相同的向量(矩阵只是一个向量,尝试c(M))然后比较每个元素与其对应的元素使用==。幸运的是==是一个泛型函数,它有一个数组方法(参见方法(“Ops”)和is.array(M)),它允许我们在其上运行rowSums(或colSums),以确保我们有数量的匹配为ncol(M)

#2


4  

Using DeMorgan's rule (Not all = Some not), then All equal = Not Some Not equal, we also have

使用DeMorgan的规则(Not all = Some not),然后All equal = Not Some Not equal,我们也有

!colSums(t(M) != v)

#3


0  

The package prodlim has a function called row.match, which is easy to use and ideal for your problem. First install and load the library: library(prodlim). In our example, row.match will return '5' because the 5th row in M is equal to v. We can then convert this into a logical vector.

包prodlim有一个名为row.match的功能,它易于使用,非常适合您的问题。首先安装并加载库:library(prodlim)。在我们的例子中,row.match将返回'5',因为M中的第5行等于v。然后我们可以将其转换为逻辑向量。

m <- row.match(v, M)
m==1:NROW(M)#[1] FALSE FALSE FALSE FALSE  TRUE

#1


7  

One possibility is

一种可能性是

rowSums(M == v[col(M)]) == ncol(M)
## [1] FALSE FALSE FALSE FALSE  TRUE

Or simlarly

rowSums(M == rep(v, each = nrow(M))) == ncol(M)
## [1] FALSE FALSE FALSE FALSE  TRUE

Or

colSums(t(M) == v) == ncol(M)
## [1] FALSE FALSE FALSE FALSE  TRUE

v[col(M)] is just a shorter version of rep(v, each = nrow(M)) which creates a vector the same size as M (matrix is just a vector, try c(M)) and then compares each element against its corresponding one using ==. Fortunately == is a generic function which has an array method (see methods("Ops") and is.array(M)) which allows us to run rowSums (or colSums) on it in order to makes sure we have the amount of matches as ncol(M)

v [col(M)]只是rep(v,each = nrow(M))的较短版本,它创建一个与M大小相同的向量(矩阵只是一个向量,尝试c(M))然后比较每个元素与其对应的元素使用==。幸运的是==是一个泛型函数,它有一个数组方法(参见方法(“Ops”)和is.array(M)),它允许我们在其上运行rowSums(或colSums),以确保我们有数量的匹配为ncol(M)

#2


4  

Using DeMorgan's rule (Not all = Some not), then All equal = Not Some Not equal, we also have

使用DeMorgan的规则(Not all = Some not),然后All equal = Not Some Not equal,我们也有

!colSums(t(M) != v)

#3


0  

The package prodlim has a function called row.match, which is easy to use and ideal for your problem. First install and load the library: library(prodlim). In our example, row.match will return '5' because the 5th row in M is equal to v. We can then convert this into a logical vector.

包prodlim有一个名为row.match的功能,它易于使用,非常适合您的问题。首先安装并加载库:library(prodlim)。在我们的例子中,row.match将返回'5',因为M中的第5行等于v。然后我们可以将其转换为逻辑向量。

m <- row.match(v, M)
m==1:NROW(M)#[1] FALSE FALSE FALSE FALSE  TRUE