Matlab Tips and Tricks 0

Introduction

Like other computer languages, M AT L A B provides operators and functions for creating and mani-
pulating arrays. Arrays may be manipulated one element at a time, like one does in low-level lan-
guages. Since M AT L A B is a high-level programming language it also provides high-level operators
and functions for manipulating arrays.
Any task which can be done in M AT L A B with high-level constructs may also be done with low-
level constructs. Here is an example of a low-level way of squaring the elements of a vector
x = [ 1 2 3 4 5 ]; % vector of values to square
y = zeros(size(x)); % initialize new vector
for i = 1 : numel(x) % for each index
y(i) = x(i)^2; % square the value
end % end of loop
and here is the high-level, or “vectorized”, way of doing the same
x = [ 1 2 3 4 5 ]; % vector of values to square
y = x.^2; % square all the values
The use of the higher-level operator makes the code more compact and more easy to read, but this is
not always the case. Before you start using high-level functions extensively, you ought to consider
the advantages and disadvantages.
1.2 Advantages and disadvantages
It is not always easy to decide when to use low-level functions and when to use high-level functions.
There are advantages and disadvantages with both. Before you decide what to use, consider the
following advantages and disadvantages with low-level and high-level code.

1.2.1 Portability
Low-level code looks much the same in most programming languages. Thus, someone who is used
to writing low-level code in some other language will quite easily be able to do the same in MATLAB.
And vice versa, low-level MATLAB code is more easily ported to other languages than high-level
MATLAB code.

1.2.2 Verbosity
The whole purpose of a high-level function is to do more than the low-level equivalent. Thus,
using high-level functions results in more compact code. Compact code requires less coding, and
generally, the less you have to write the less likely it is that you make a mistake. Also, is is more
easy to get an overview of compact code; having to wade through vast amounts of code makes it
more easy to lose the big picture.
1.2.3 Speed
Traditionally, low-level M AT L A B code ran more slowly than high-level code, so among M AT L A B
users there has always been a great desire to speed up execution by replacing low-level code with
high-level code. This is clearly seen in the M AT L A B newsgroup on Usenet, comp.soft-sys.matlab,
where many postings are about how to “translate” a low-level construction into the high-level equi-
valent.
In M AT L A B 6.5 an accelerator was introduced. The accelerator makes low-level code run much
faster. At this time, not all code will be accelerated, but the accelerator is still under development
and it is likely that more code will be accelerated in future releases of M AT L A B. The M AT L A B
documentation contains specific information about what code is accelerated.
1.2.4 Obscurity
High-level code is more compact than low-level code, but sometimes the code is so compact that
is it has become quite obscure. Although it might impress someone that a lot can be done with a
minimum code, it is a nightmare to maintain undocumented high-level code. You should always
document your code, and this is even more important if your extensive use of high-level code makes
the code obscure.
1.2.5 Difficulty
Writing efficient high-level code requires a different way of thinking than writing low-level code. It
requires a higher level of abstraction which some people find difficult to master. As with everything
else in life, if you want to be good at it, you must practice.

1.3 Words of warning
Don’t waste your time. Don’t rewrite code which doesn’t need rewriting. Don’t optimize code before
you are certain that the code is a bottleneck. Even if the code is a bottleneck: Don’t spend two hours
reducing the execution time of your program by one minute unless you are going to run the program
so many times that you will save the two hours you spent optimizing it in the first place.