Skip to content

Re turn values < 0 from Matrix

2 messages · Ian Fiske, David Winsemius

#
If your matrix is called mat, how about

mat[which(mat[,2] > 0), ]


mat[which(mat[,2] < 0), ]


-Ian
mentor_ wrote:

  
    
#
I still remember my public spanking from Ben Bolker on the unnecessary  
use of "which" in this instance.

 > MM <- matrix(c(1:10,sample(-10:10,10)),nrow=10)
 > MM
       [,1] [,2]
  [1,]    1   -1
  [2,]    2    5
  [3,]    3   -2
  [4,]    4   -3
  [5,]    5    0
  [6,]    6    7
  [7,]    7   -9
  [8,]    8    1
  [9,]    9    6
[10,]   10    4

 > MM[MM[,2]<0, ]
      [,1] [,2]
[1,]    1   -1
[2,]    3   -2
[3,]    4   -3
[4,]    7   -9

 > MM[MM[,2]>0, ]
      [,1] [,2]
[1,]    2    5
[2,]    6    7
[3,]    8    1
[4,]    9    6
[5,]   10    4