[R] threshold matrix

Rolf Turner rolf.turner at xtra.co.nz
Sat Apr 30 03:07:54 CEST 2011


On 30/04/11 02:44, 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?

(1) M2 <- 0 +(M >= thresh)

is one line (and is sexier! :-) ).

(2) which(A !=B, arr.ind = TRUE)

or (possibly more sensibly)

     which (abs(A-B) > sqrt(.Machine$double.eps), arr.ind = TRUE)

should solve your ``One more question''.

     cheers,

         Rolf Turner



More information about the R-help mailing list