Updating an array inside a for loop- comparison with MATLAB

thanks yuyichao.
you’re right that about 75% of the time is spent on the L*pos
I’m preallocating the pos vector, just didn’t include it in the code above.
In addition, is it normal to have about 800Mb of memory allocation for these kind of loops? measured with @time by running a function wrapped around this loops with pos as an input