Hi,
A good approach to study the performance of the above code would be to use a profiler. The profiler would expose the computationally expensive areas of the code. One can then concentrate on these areas to further improve the performance of the code.
To profile this code use the 'Run and Time' button in the Editor tab. I have used the value of 'n' as 1000 here. On profiling I observed that the inner loop 'k' is most expensive computationally. On vectorizing this, I observed a significant improvement in the performance. Check the attached codes for the illustration. I observed the code to execute seven times faster at my end due to this modification.
Refer to the following MATLAB documentation links to learn more about Measuring and Improving performance of your code:
Further to improve the performance of the code, one can parallelize the code using PARFOR. However, to use PARFOR you would need a Parallel Computing Toolbox.
Refer to the following MATLAB documentation links to learn more about PARFOR function:
Refer to the following MATLAB documentation links to learn more about Parallel Computing Toolbox: