As was suggested avoiding loops and vectorization is one way to speed-up matlab code (you can also use "parfor" to parallelize your code); Your code seems to be simple enough to allow such remedies. However there are instances that the structure of your loop is complicated and it's not possible to easily avoid loops or you may encounter memory issues for "very" large matrices.
Another less known capability of matlab is c/c++ and fortran interoperability with mex functions.
Attached is a mex function for your simple loop, assuming your mex command is configured correctly (read the documentation, you need a c or fortran compiler and need to use mex -setup); do the following
>> mex dist.c
Building with 'gcc'.
MEX completed successfully.
(You should now have a dist.mexa64 in linux or dist.dll in windows)
I know at least 1,001: Accelerating MATLAB Performance: 1001 tips to speed up MATLAB programs by Altman. To give you an idea I've attached/uploaded an excerpt (there must be a more efficient way to scan pages). The price tag is a bit hefty, at least for me, but if you are not neurotic as I am and you are willing to use the library to obtain a book loaned to you rather than one you own (as I am not willing to), then I'd recommend seeing if you can get it through your library or inter-library loan.
If you start out new, I would not recommend trying to vectorize your code, since it often is much less readable. Once your program runs, you can try to do that and that WILL significantly speed up your code. But it takes some time to accommodate to vectorized code and you need proper knowledge of your problem. In addition, vectorized code might need a lot more memory (since one tends to put everything in large matrices).
Looking at your example, much easier modifications will also speed up the code. If you have to use a loop, do as much outside the loop as possible and preallocate arrays (see below). Finally, there is often a ML function for stuff you want to do, so thorough search in the help browser is also recommended.
For your example, one for loop can be skipped by using REPMAT.
As was suggested avoiding loops and vectorization is one way to speed-up matlab code (you can also use "parfor" to parallelize your code); Your code seems to be simple enough to allow such remedies. However there are instances that the structure of your loop is complicated and it's not possible to easily avoid loops or you may encounter memory issues for "very" large matrices.
Another less known capability of matlab is c/c++ and fortran interoperability with mex functions.
Attached is a mex function for your simple loop, assuming your mex command is configured correctly (read the documentation, you need a c or fortran compiler and need to use mex -setup); do the following
>> mex dist.c
Building with 'gcc'.
MEX completed successfully.
(You should now have a dist.mexa64 in linux or dist.dll in windows)