Skip to content
Prev 106146 / 398506 Next

matrix - change values

robert-mcfadden at o2.pl wrote:
The same syntax as for a vector:

A[A>5] <- 0

Remember that matrices are just vectors with a dim attribute.  The dim 
attribute is unchanged by this operation:

 > A <- matrix(1:10, 2, 5)
 > A
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10
 > A[A>5] <- 0
 > A
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    0    0
[2,]    2    4    0    0    0

Duncan Murdoch