there is a very cool application in matlab to help you improve the code. it is called "profiler".
let me give you an example. If you have a script named "name_of_script.m", you can enter the following commands to check it:
at the end, you'll get a new windows, where you can find which section of the script is the bottleneck.