you have a lot of efficient matlab code to do such things in a vectorized way here:

https://github.com/agramfort/matlab_mesh_toolbox

see mesh_* functions