Dear all, I have a quite big matrix which I would like to threshold. If the value is below threshold the cell should be zero and if the value is over threshold the cell should be one One really simple way to do that is two have a nested loop and check cell by cell. The problem is that this seems to be really time consuming and ineficient. What do you suggest me to try out? I would like to thank you in advance for your help Best Regards Alex
On Apr 29, 2011, at 9:37 AM, Alaios wrote:> Dear all, > I have a quite big matrix which I would like to threshold. > If the value is below threshold the cell should be zero > and > if the value is over threshold the cell should be oneM2 <- M M2[M < thresh] <- 0 M2[M >= thresh] <- 1 or perhaps simply: M2 <- as.numeric( M[] < thresh )> > One really simple way to do that is two have a nested loop and check > cell by cell. > > The problem is that this seems to be really time consuming and > ineficient. > > What do you suggest me to try out?-- David Winsemius, MD West Hartford, CT
Thanks a lot. I finally used M2 <- M M2[M < thresh] <- 0 M2[M >= thresh] <- 1 as I noticed that this one line M2 <- as.numeric( M[] < thresh ) vectorizes my matrix. One more question I have two matrices that only differ slightly. What will be the easiest way to compare and find the cells that are not the same? Best Regards Alex --- On Fri, 4/29/11, David Winsemius <dwinsemius at comcast.net> wrote:> From: David Winsemius <dwinsemius at comcast.net> > Subject: Re: [R] threshold matrix > To: "Alaios" <alaios at yahoo.com> > Cc: R-help at r-project.org > Date: Friday, April 29, 2011, 2:57 PM > > On Apr 29, 2011, at 9:37 AM, Alaios wrote: > > > Dear all, > > I have a quite big matrix which I would like to > threshold. > > If the value is below threshold the cell should be > zero > > and > > if the value is over threshold the cell should be one > > M2 <- M > M2[M < thresh] <- 0 > M2[M >= thresh] <- 1 > > or perhaps simply: > > M2 <- as.numeric( M[] < thresh ) > > > > One really simple way to do that is two have a nested > loop and check cell by cell. > > > > The problem is that this seems to be really time > consuming and ineficient. > > > > What do you suggest me to try out? > > -- > David Winsemius, MD > West Hartford, CT > >
On Fri, Apr 29, 2011 at 07:44:59AM -0700, Alaios wrote:> Thanks a lot. > I finally used > > M2 <- M > M2[M < thresh] <- 0 > M2[M >= thresh] <- 1 > > as I noticed that this one line > > M2 <- as.numeric( M[] < thresh ) > vectorizes my matrix.Hi. This may be avoided, for example M2 <- M M2[, ] <- as.numeric(M >= thresh) or array(as.numeric(M >= thresh), dim=dim(M))> One more question I have two matrices that only differ slightly. What will be the easiest way to compare and find the cells that are not the same?If A and B are matrices of the same dimension, then A == B is a logical matrix with TRUE entires for positions, where A and B match exactly. abs(A - B) <= eps is a logical matrix with TRUE entires for positions, where A and B differ at most by eps. If you want to get only one logical result, then use all(A == B) for exact equality and all(abs(A - B) <= eps) for approximate equality of all entries. See also ?all.equal, which uses the relative error, not absolute difference. Hope this helps. Petr Savicky.
On Apr 29, 2011, at 10:44 AM, Alaios wrote:> Thanks a lot. > I finally used > > M2 <- M > M2[M < thresh] <- 0 > M2[M >= thresh] <- 1 > > as I noticed that this one line > > M2 <- as.numeric( M[] < thresh ) > vectorizes my matrix. > > One more question I have two matrices that only differ slightly. > What will be the easiest way to compare and find the cells that are > not the same?M[!M==N] N[!M==N]> > Best Regards > Alex > > --- On Fri, 4/29/11, David Winsemius <dwinsemius at comcast.net> wrote: > >> From: David Winsemius <dwinsemius at comcast.net> >> Subject: Re: [R] threshold matrix >> To: "Alaios" <alaios at yahoo.com> >> Cc: R-help at r-project.org >> Date: Friday, April 29, 2011, 2:57 PM >> >> On Apr 29, 2011, at 9:37 AM, Alaios wrote: >> >>> Dear all, >>> I have a quite big matrix which I would like to >> threshold. >>> If the value is below threshold the cell should be >> zero >>> and >>> if the value is over threshold the cell should be one >> >> M2 <- M >> M2[M < thresh] <- 0 >> M2[M >= thresh] <- 1 >> >> or perhaps simply: >> >> M2 <- as.numeric( M[] < thresh ) >>> >>> One really simple way to do that is two have a nested >> loop and check cell by cell. >>> >>> The problem is that this seems to be really time >> consuming and ineficient. >>> >>> What do you suggest me to try out? >> >> -- >> David Winsemius, MD >> West Hartford, CT >> >>David Winsemius, MD West Hartford, CT