Accelerating Matlab

Matlab is a very useful programming environment, but it also has many inefficiencies. You might think that these are unavoidable, but in fact it is possible to fix most of them, without significantly changing your programs. This page describes some easy ways to modify the Matlab environment to make programs run faster.

  1. Install Marcel Leutenegger‘s package of elementary functions. They speed up functions like exp and log by a factor of 3 or more, with no loss of accuracy. They directly replace the functions in Matlab, so no program modification is required.
  2. Run mex -setup and select a good compiler. The default compiler (lcc) does not produce very good code.

    To use Microsoft Visual Studio .NET 2003 version 7.1, you will first need to install a patch. Unfortunately, while Visual Studio 7.1 generally produces good code, it has a performance bug in the intrinsic exp function. To get around this, edit the mex options file (C:\MATLAB6p5p1\bin\win32\mexopts\msvc71opts.bat) to read:

    set OPTIMFLAGS=/MD -O2 -Oy- /Oi- -DNDEBUG

    After changing the compiler, you should re-compile your mex files, and re-install any packages including mex (such as lightspeed).

  3. Install lightspeed. It provides optimized implementations of common operations, including a C replacement for repmat.m.
  4. Profile your code to find bottlenecks:
    profile on
    myfun;
    profile report
    
  5. Avoid loops by writing ‘vectorized‘ code. See the MathWorks‘ Vectorization Guide, Marios Athineos‘s tips and tricks, and the routines in lightspeed (such assqdist).

Efficient ways to do common tasks

Manipulate sets of integers

The fastest way to do this is with sparse logical vectors. If you want to use sorted arrays of integers instead, beware that the Matlab functions setdiff, union, etc. are not optimized for this case and will be a bottleneck. Optimized functions for the sorted case are included in lightspeed.

Represent a graph

Use a sparse logical adjacency matrix, and use matrix operations whenever possible. For example, if G is symmetric (i.e. an undirected graph) then G*G gives the number of neighbors in common to nodes i and j, for all (i,j). See Kevin Murphy‘s graph toolbox.

Sample random numbers from various distributions

Use the functions provided in lightspeed.

Read XML

Peter Rysadter‘s XML parser was the fastest, but is no longer available. Check out the links at Undocumented XML functionality.

文章转载自 Tom MinkaAccelerating Matlab

时间: 2024-10-10 05:14:13

Accelerating Matlab的相关文章

Matlab tips and tricks

matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it grew to become my annotated Matlab reading cache. In order to motivate the DSP people out there, I am showing below how one can apply a window and scal

利用MATLAB进行曲线拟合

软件环境:MATLAB2013a 一.多项式拟合 多项式拟合是利用多项式最佳地拟合观测数据,使得在观测数据点处的误差平方和最小. 在MATLAB中,利用函数ployfit和ployval进行多项式拟合. 函数ployfit根据观测数据及用户指定的多项式阶数得到光滑曲线的多项式表示,polyfit的一般调用格式为:P = polyfit(x,y,n).其中x为自变量,y为因变量,n为多项式阶数. polyval的输入可以是标量或矩阵,调用格式为 pv = polyval(p,a) pv = pol

MATLAB检查指定路径中的子文件夹中的文件名中是否带有空格

测试文件夹为: clear;close all;clc; %% %程序实现的功能 %检查指定路径中的子文件夹中的文件名中是否带有空格,并去掉文件名中的空格 %% %程序中用到的之前不清楚的函数如下 %1)strfind(a,b):即找a中是否有b,如果a中有b,则输出b的位置序号.没有输出空数组 %2)isempty(a):判断数组是否为空 %3)strrep(a,b,c):就是把a中所有出现的b换为c %4)movefile(a,b):a移动为b,如C:\test1.jpg移动为C\test2

关于MATLAB处理大数据坐标文件2017620

暑假已至,接下来组内成员将会各回各家,各找各妈,这肯定是对本次大数据比赛是很不利的. 接下来我会把任务分配给组员,当然任务会比起初的时候轻一点,因为我认为本次比赛的目的并不是我要求组员做什么,而是我的组员要求自己做什么! 我们现在主要接触的两门语言: MATLAB语言在数据处理方面很牛,它的画图功能也是杠杠的,尤其是3D画图 Python语言是一门近几年很火的语言,学好它对自己肯定只有益处,它的出生很晚,但是短短十多年,它已经稳居计算机语言前三名.尤其是现在的大数据时代,它的代码不仅简单易懂,而

linux用命令行运行matlab的.mat文件

入m文件所在目录后,运行 $ matlab -nodesktop -nosplash -r matlabfile 只用文件名matlabfile,不能添加.m

对AM信号FFT的matlab仿真

普通调幅波AM的频谱,大信号包络检波频谱分析 u(t)=Ucm(1+macos ?t)cos ?ct ma称为调幅系数 它的频谱由载波,上下边频组成 , 包络检波中二极管截去负半周再用电容低通滤波,可以得到基带信号,那么,截去负半周后的AM信号必定包含基带信号的频谱.我们可以通过matlab来验证. %已知基带信号为1hz,载波为64hz,调制系数ma=0.3,采样频率1024hz,FFT变换区间N为2048 clear; fs=1024; f=1; %1hz基带信号 fc=64; %64hz载

Matlab中使用jython扩展功能

Matlab中面向对象能力并不强,通过使用jython引擎能够对其功能扩展. 1 编辑classpath.txt增加jython.jar 在matlab中输入 which classpath.txt 结果: /usr/local/MATLAB/R2013a/toolbox/local/classpath.txt 编辑该文件,加入 /home/your_user/jython2.5.3/jython.jar 2 又一次启动matlab 3 编写代码測试 import javax.script.In

MATLAB(5)——生成归一化直方图

作者:桂. 时间:2017-03-10  22:13:36 链接:http://www.cnblogs.com/xingshansi/p/6533579.html 声明:欢迎转载,不过记得注明出处哦~ 前言 本文作为:曲线拟合与分布拟合 一文的补充内容,主要介绍MATLAB直方图的绘制,以及对应归一化直方图的实现.全文分三部分简单介绍: 1)直方图(hist)绘制: 2)栅栏图(bar)绘制: 3)归一化直方图实现. 一.直方图(hist) 可以对hist的直方图进行限定,两种途径:个数模式(n

Matlab:线性与非线性规划

Matlab的运筹与决策问题 线性规划问题 函数: linprog(f,A,B,Aep,Bep,lb,ub) 参数分析: f:目标函数的系数排列 A:约束条件的系数矩阵 B:约束条件的增广矩阵的结果 Aep:等式的系数矩阵 Bep:等式的结果矩阵 lb:所求解的最小值 ub:所求解的最大值 非线性规划问题(二次) 函数 quadprog(f,A,B,Aep,Bep,lb,ub) 非线性规划问题(普遍性) 函数 x=fmincon(’fun’,X0,A,b,Aeq,Beq,VLB,VUB,’non