Skip to content

Instantly share code, notes, and snippets.

@Choumingzhao
Last active September 4, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Choumingzhao/39c05ae0ed735b3052bdf078982526a5 to your computer and use it in GitHub Desktop.
Save Choumingzhao/39c05ae0ed735b3052bdf078982526a5 to your computer and use it in GitHub Desktop.
MATLAB按元素相乘运算速度比较

众所周知,MATLAB长于矩阵运算,在使用矩阵进行运算时比普通的循环快得多,同时,不同的矩阵运算速度也会有不同。以下为实例:

>> clear all  
>> a = rand(100000000, 1);  
>> tic; a'*a; toc % 方法一,矩阵乘法  
时间已过 0.054834 秒。  
>> tic; a.*a; toc % 方法二,不包括求和  
时间已过 0.211555 秒。  
>> tic; sum(a.*a); toc % 方法二,包括求和  
时间已过 0.447394 秒。 

可以看出,使用矩阵式的乘法相比于使用element-wise的方法在速度上有明显优势的,在使用MATLAB中如果对效率要求比较高的话,应该多加注意本项。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment