r - Replacing negative numbers with zero in a matrix -


so today coding wanted replace negative values in matrix 0. call matrix b. no problem, wrote

b[which(b<0)]=0 

but because curious wondering, if got rid of , wrote

b[b<0]=0 

and surprise gave same answer. if have looked question on stack overflow second answer pretty standard (and there more complicated faster methods), question is: 2 methods above same? b<0 returns boolean matrix. method faster , why?

yes, can index logical vector. it's explained here: http://cran.r-project.org/doc/manuals/r-release/r-intro.html#index-vectors


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -