Vector programming

1.5.2 Vector programming

It is this author’s experience that MATLAB code written using vector addressing and linear algebra constructs can be quite efficient. In fact run times can approach that of compiled C or Fortran. On the other hand, coding in the scalar style can produce very slow programs that give correct results but lead to the false impression that MATLAB is a slow environment. MATLAB is designed to eliminate many of the loop structures found in other languages. Vector addressing helps in this regard but even more important is the use of MATLAB’s linear algebra constructs. Programming efficiently in this way is called vector programming.

As an example, suppose seis is a seismic data matrix and it is desired to scale each trace (i.e. column) by a constant scale factor. Let scales be a vector of such factors whose length is equal to the number of columns in seis. Using scalar programming, a seasoned Fortran programmer might produce something like Code Snippet 1.5.4.

Code Snippet 1.5.4.

A Fortran programmer might write this code to scale each trace in the matrix seis bya factor from the vector scales

1 [nsamp,ntr]=size(seis); 2

3 for col=1:ntr

4 for row=1:nsamp

5 seis(row,col)=scales(col)*seis(row,col);

6 end

7 end

26 CHAPTER 1. INTRODUCTION

End Code

This works correctly but is needlessly slow. An experienced MATLAB programmer would know that the operation of matrix-times-diagonal matrix results in a new matrix whose columns are scaled by the corresponding element of the diagonal matrix. You can check this out for yourself by a simple MATLAB exercise:

a=ones(4,3); b=diag([1 2 3]); a*b to which MATLAB’s response is ans=

1 2 3 Thus the MATLAB programmer would write Code Snippet 1.5.4 with the very simple single line

in Code Snippet 1.5.5. Code Snippet 1.5.5.

A MATLAB programmer would write the example of Code Snippet 1.5.4 in this way:

1 seis=seis*diag(scales);

End Code

Tests indicate that Code Snippet 1.5.5 is about ten times as fast as Code Snippet 1.5.4. Vector programming is also facilitated by the fact that MATLAB’s mathematical functions are automatically set up to operate on arrays. This includes functions like sin, cos, tan, atan, atan2, exp, log, log10, sqrt and others. For example, if t is a time coordinate vector then sin(2*pi*10*t) is a vector of the same size as t whose entries are a 10 Hz. sine wave evaluated at the times in t. These functions all perform element-by-element operations on matrices of any size. Code written in this way is actually more visually similar to the corresponding mathematical formulae than scalar code with its many explicit loops.

Some of MATLAB’s vectorized functions operate on matrices but return matrices of one di- mension smaller. The functions sum, cumsum, mean, main, max, std all behave in this manner. For example, if trace is a vector and seis is a matrix, then mean(trace) results in a scalar that is the mean value of the vector while mean(seis) results in a row vector containing the mean value of each column of seis. The mean value of the entire matrix, seis, can be calculated with mean(mean(seis)) .