登录论坛

查看完整版本 : 在MATLAB中进行向量化简介-有什么好的教程吗?


poster
2019-12-10, 20:41
我正在寻找有关MATLAB中向量化(循环)的任何优秀教程。

我有一个非常简单的算法,但是它使用两个for循环。我知道将其向量化应该很简单,我想学习如何做到这一点,而不是要求您提供解决方案。

但是,为了让您知道我遇到了什么问题,因此您将能够建议最好的教程来展示如何解决类似的问题,这是我的问题的概述:

B = zeros(size(A)); % //A is a given matrix. for i=1:size(A,1) for j=1:size(A,2) H = ... %// take some surrounding elements of the element at position (i,j) (ie using mask 3x3 elements) B(i,j) = computeSth(H); %// compute something on selected elements and place it in B end end 因此,我不要求解决方案。我要一个好的教程,MATLAB中向量化循环的示例。我想学习如何做以及自己做。



回答:

这是我经常链接为该主题的参考的几个MathWorks教程:


代码向量化指南 (http://www.mathworks.com/support/tech-notes/1100/1109.html)
提高性能的技术:向量化循环 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html#br8fs0d-1)
这是Loren的博客文章之一,针对特定的示例问题提供了很好的代码矢量化演练:


加速MATLAB应用 (http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/)
您作为样本给出的特定类型的问题,涉及处理给定矩阵的子矩阵,可以根据您所执行的哪种操作以不同的方式将其向量化。您可能可以使用CONV2 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/conv2.html)或FILTER2 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/filter2.html)而不是嵌套的for循环。 图像处理工具箱 (http://www.mathworks.com/access/helpdesk/help/toolbox/images/)中还有许多功能可以处理矩阵的邻域和块处理 (http://www.mathworks.com/access/helpdesk/help/toolbox/images/f3-23960.html#f3-23645) ,例如NLFILTER (http://www.mathworks.com/access/helpdesk/help/toolbox/images/nlfilter.html)和BLOCKPROC (http://www.mathworks.com/access/helpdesk/help/toolbox/images/blockproc.html) 。这些功能的文档应帮助您弄清楚如何使用它们作为矢量化代码的方式。



更多&回答... (https://stackoverflow.com/questions/2867901)