Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I inverse the binary digits (0 and 1) of a matrix in R?

Tags:

r

I have created a matrix of binary digits from reading in a pgm file using the R package pixmap but I am trying to inverse the binary digits (making all 0s become 1s and all 1s become 0s) and outputting the matrix again. Is there an efficient way of doing this?

like image 384
Lynda Avatar asked Nov 20 '25 02:11

Lynda


1 Answers

matrix <- +(!matrix)

The ! coerces it into a logical matrix and switches the 0s and 1s to TRUE and FALSE. The + coerces it back to 0s and 1s.

like image 119
Cole Avatar answered Nov 22 '25 16:11

Cole