1、尽量避免使用循环,MATLAB的文档中写到“MATLAB is a matrix language, whic h means it is designed for vector and matrix operations. You can often speed up your M-file c ode by using vectorizing algorithms that take advantage of this design. Vectorizati on means converting for and while loops to equivalent vector or matrix operations.”。改进 这样的状况有两种方法:
a、尽量用向量化的运算来代替循环操作。如将下面的程序: for t = 0:.01:10 i = i+1; y(i) = sin(t); end 替换为: t = 0:.01:10; y = sin(t); 速度将会大大加快。最常用的使用vectorizing技术的函数有:All、diff、i permute、permute、