MATLAB: How to reduce the time of calling functions

code optimization

Best Answer

2/3 of the time is spent executing obj() which is not a Mathworks supplied function. You should investigate obj() to see how it could be sped up.
1/3 of the time is spent executing squeeze(), which is a Mathworks supplied function. As you are calling it many times in a loop, chances are good that you know which dimensions are expected to be singular, and in that situation there is a fairly strong chance that you can replace the call to squeeze() with a call to reshape() which would be more efficient because it would not need to do tests on the dimensions. The savings are probably not all that high though.
Related Question