How to if_else the elements of a matrix

Hello Stan gurus,

Is there any efficientent way of writting in Stan e.g.:

M <- matrix(1:9,3,3)
I <- ifelse(M < 5, 1, 0)

Or I need a double loop?

Thanks in advance

There’s no builtin method for that but (unlike R) Stan is a complied language and loops are quite efficient.


I think you don’t need a double loop. You can use a foreach loop.

1 Like

Thank you @nhuurre and @Max_Mantei for your prompt answers. That makes sense.