|
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by LosemindL
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 <[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? -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
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-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
On Jun 12, 2012, at 1:58 PM, David Winsemius wrote: > > 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) I see I failed to transpose the line that created wM wM <- which(MASK, arr.ind=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-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
| Powered by Nabble | Edit this page |
