'R: given a list of matrices, determine which ones have a specified value in a specific element
I have a list L of length k whose elements are matrices of equal dimension n x m. Suppose I want to know which of the matrices in the list satisfy the condition M[i,j]==1.
Obviously,
grep(1, L)
will not work as it will return all instances of 1 in the matrices, rather than just those where 1 occurs in position i,j. Is there any way to do this other than using a for loop and checking each matrix individually?
As a simple example, if I have a list of three matrices:
m1 = rbind(c(0,0,0),c(0,0,0),c(1,1,1)
m2 = rbind(c(1,1,1),c(1,0,0),c(0,0,0)
m3 = rbind(c(0,0,0),c(0,1,0),c(1,1,1)
mlist = list(m1,m2,m3)
I would like to find for which matrix/matrices in mlist M[2,2]=1, the correct output would be 3, since the third matrix in the list satisfies this condition.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
