Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is your favourite MATLAB/Octave programming trick? [closed]

Tags:

matlab

octave

People also ask

Which is best Matlab or Octave?

MATLAB is probably a lot more powerful than Octave, and the algorithms run faster, but for most applications, Octave is more than adequate and is, in my opinion' an amazing tool that is completely free, where Octave is completely free.

Is Octave like Matlab?

Now try this with Octave. As you've written: "The language [Octave] is almost identical to basic Matlab." When using _advanced_ instead of _basic_ Matlab features, the differences between these platforms become more considerable.

Is Octave a popular programming language?

Coming to the popularity standards, Octave is not that prominent in the public. The other programming languages are highly popular in the market and have massive community support. Also, the adaptability rate of the all these three is quite high as compared to octave for machine learning.


Using the built-in profiler to see where the hot parts of my code are:

profile on
% some lines of code
profile off
profile viewer

or just using the built in tic and toc to get quick timings:

tic;
% some lines of code
toc;

Directly extracting the elements of a matrix that satisfy a particular condition, using logical arrays:

x = rand(1,50) .* 100;
xpart = x( x > 20 & x < 35);

Now xpart contains only those elements of x which lie in the specified range.


Provide quick access to other function documentation by adding a "SEE ALSO" line to the help comments. First, you must include the name of the function in all caps as the first comment line. Do your usual comment header stuff, then put SEE ALSO with a comma separated list of other related functions.

function y = transmog(x)
%TRANSMOG Transmogrifies a matrix X using reverse orthogonal eigenvectors
%
% Usage:
%   y = transmog(x)
%
% SEE ALSO
% UNTRANSMOG, TRANSMOG2

When you type "help transmog" at the command line, you will see all the comments in this comment header, with hyperlinks to the comment headers for the other functions listed.


Turn a matrix into a vector using a single colon.

x = rand(4,4);
x(:)

Vectorizing loops. There are lots of ways to do this, and it is entertaining to look for loops in your code and see how they can be vectorized. The performance is astonishingly faster with vector operations!


Anonymous functions, for a few reasons:

  1. to make a quick function for one-off uses, like 3x^2+2x+7. (see listing below) This is useful for functions like quad and fminbnd that take functions as arguments. It's also convenient in scripts (.m files that don't start with a function header) since unlike true functions you can't include subfunctions.
  2. for closures -- although anonymous functions are a little limiting as there doesn't seem to be a way to have assignment within them to mutate state.

.

% quick functions
f = @(x) 3*x.^2 + 2*x + 7;
t = (0:0.001:1);
plot(t,f(t),t,f(2*t),t,f(3*t));

% closures (linfunc below is a function that returns a function,
% and the outer functions arguments are held for the lifetime
% of the returned function.
linfunc = @(m,b) @(x) m*x+b;
C2F = linfunc(9/5, 32);
F2C = linfunc(5/9, -32*5/9);

Matlab's bsxfun, arrayfun, cellfun, and structfun are quite interesting and often save a loop.

M = rand(1000, 1000);
v = rand(1000,    1);
c = bsxfun(@plus, M, v);

This code, for instance, adds column-vector v to each column of matrix M.

Though, in performance critical parts of your application you should benchmark these functions versus the trivial for-loop because often loops are still faster.