My julia code is somehow much slower than the matlab code

If I’m reading this line correctly, you’re setting k_index equal to a datatype and then in the hot loop you’re changing its value to an instance of that type.