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、尽量用向量化的运算来代替循环操作。如将下面的程序:
i=0; 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、 reshape、squeeze、any、find、logical、prod、shiftdim、sub2ind、cums um、ind2sub、 ndgrid、repmat、sort、sum 等。
请注意matlan文档中还有这样一句补充:“Before taking the time to
vectorize your code, read the section on Performance Acceleration. You may be able to speed up your program by just as much using the MATLAB JIT Accelera tor instead of vectorizing.”。何去何从,自己把握。