Hi all, Lets say I have a matrix A which is m x n. I also have a mask matrix MASK which is m x n with values in T/F, where T values make a sub-matrix in regutangular shape... I applied B=A[MASK] and it didn't work as expected... Any thoughts? [[alternative HTML version deleted]]
Hello, On Tue, Jun 12, 2012 at 12:44 PM, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > Lets say I have a matrix A which is m x n. > > I also have a mask matrix MASK which is m x n with values in T/F, where T > values make a sub-matrix in regutangular shape... > > I applied B=A[MASK] and it didn't work as expected... > > Any thoughts?What did you expect and what happened? I'm assuming you did: B <- A[MASK] and I would expect the the result would be a vector whose size is the number of elements in MASK that is TRUE. To get a matrix, you then set the dim attribute (but I didn't test that). Take care Oliver -- Oliver Ruebenacker Bioinformatics Consultant (http://www.knowomics.com/wiki/Oliver_Ruebenacker) Knowomics, The Bioinformatics Network (http://www.knowomics.com) SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
Providing a reproducible example is a good idea, but what about this:> A <- matrix(1:20, nrow=4) > MASK <- matrix(FALSE, nrow=4, ncol=5) > MASK[2:3, 1:3] <- TRUE > MASK[,1] [,2] [,3] [,4] [,5] [1,] FALSE FALSE FALSE FALSE FALSE [2,] TRUE TRUE TRUE FALSE FALSE [3,] TRUE TRUE TRUE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE> A[rowSums(MASK) > 0, colSums(MASK) > 0][,1] [,2] [,3] [1,] 2 6 10 [2,] 3 7 11 Alternatively, you could take the vector returned by:> A[MASK][1] 2 3 6 7 10 11 and put it back into matrix format. Sarah On Tue, Jun 12, 2012 at 12:44 PM, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > Lets say I have a matrix A which is m x n. > > I also have a mask matrix MASK which is m x n with values in T/F, where T > values make a sub-matrix in regutangular shape... > > I applied B=A[MASK] and it didn't work as expected... > > Any thoughts?-- Sarah Goslee http://www.functionaldiversity.org
On Tue, Jun 12, 2012 at 11:44:51AM -0500, Michael wrote:> Hi all, > > Lets say I have a matrix A which is m x n. > > I also have a mask matrix MASK which is m x n with values in T/F, where T > values make a sub-matrix in regutangular shape... > > I applied B=A[MASK] and it didn't work as expected...Hi. Try the following. A <- matrix(1:16, nrow=4, ncol=4) B <- matrix(FALSE, nrow=4, ncol=4) B[2:3, 2:3] <- TRUE A[rowSums(B) != 0, colSums(B) != 0] [,1] [,2] [1,] 6 10 [2,] 7 11 Hope this helps. Petr Savicky.
On Jun 12, 2012, at 12:44 PM, Michael wrote:> Hi all, > > Lets say I have a matrix A which is m x n. > > I also have a mask matrix MASK which is m x n with values in T/F, > where T > values make a sub-matrix in regutangular shape... > > I applied B=A[MASK] and it didn't work as expected...Perhaps: (not fully tested in absence of reproducible example): B=A[MASK] MASK <- matrix(as.logical( c(0,0,0,0,0, 0,1,1,1,0, 0,1,1,1,0, 0,0,0,0,0, 0,0,0,0,0)), 5, byrow=TRUE) #---- row col [1,] 2 2 [2,] 3 2 [3,] 2 3 [4,] 3 3 [5,] 2 4 [6,] 3 4 #----- B <- matrix(B, nrow=1+diff(range(wM[,"row"])), ncol=1+diff(range(wM[,"col"])) ) -- David Winsemius, MD West Hartford, CT