So today I was coding up something where I wanted to replace all negative values in a matrix with 0. Call this matrix B. Well this was no problem, I just wrote
B[which(B<0)]=0
But then just because I was curious I was wondering, what if we got rid of the which and wrote
B[B<0]=0
and to my surprise this also gave the same answer. If I would have looked up this question on Stack Overflow the second answer is pretty standard (and there are even more complicated faster methods), but my question is: are the two methods above actually the same? B<0 returns a Boolean matrix. Which method is faster and why?