# How to subset a matrix?

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## How to subset a matrix?

 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to subset a matrix?

 Hello, On Tue, Jun 12, 2012 at 12:44 PM, Michael <[hidden email]> 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) ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to subset a matrix?

Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to subset a matrix?

 In reply to this post by LosemindL 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to subset a matrix?

 In reply to this post by LosemindL 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.