I'm hoping to vectorize the following loop:
for (i in 1:n) {
for (j in 1:m) {
temp_mat[i,j]=min(temp_mat[i,j],1);
}
}
I thought I could do temp_mat=min(temp_mat,1)
, but this is not giving me the desired result. Is there a way to vectorize this loop to make it much faster?
The question is slightly confusing because
min()
is vectorized. In order to obtain the desired result in this specific case, it is however not necessary to use this function. Logical subsetting provides a probably more efficient (and certainly more compact) alternative.If I understood your desired output correctly, a modification of the matrix as performed by the nested loops in your code can be achieved with a single command:
Hope this helps.
Just use
temp_mat <- pmin(temp_mat, 1)
. See?pmin
for more use of parallel minima.Example:
update
Since you have background in computational science, I would like to provide more information.
pmin
is fast, but is far from high performance. Its prefix "parallel" only suggestselement-wise
. The meaning of "vectorization" in R is not the same as "SIMD vectorization" in HPC. R is an interpreted language, so "vectorization" in R means opting for C level loop rather than R level loop. Therefore,pmin
is just coded with a trivial C loop.Real high performance computing should benefit from SIMD vectorization. I believe you know SSE/AVX intrinsics. So if you write a simple C code, using
_mm_min_pd
fromSSE2
, you will get ~2 times speedup frompmin
; if you see_mm256_min_pd
from AVX, you will get ~4 times speedup frompmin
.Unfortunately, R itself can not do any SIMD. I have an answer to a post at Does R leverage SIMD when doing vectorized calculations? regarding this issue. For your question, even if you link your R to a HPC BLAS,
pmin
will not benefit from SIMD, simply becausepmin
does not involve any BLAS operations. So a better bet is to write compiled code yourself.