Matlab Array Notation and Operations


One powerful feature of Matlab is its use of array notation. This allows you to reference or assign to subarrays, take "slices" of arrays, reshape arrays, and permute vectors. Matlab essentially does this by letting you use a vector as index anywhere a standard language like C/C++ or Fortran would use a scalar integer.

Beyond making it easy to deal with a large amount of data succintly, array operations allow a compiler to make optimizations that a loop-based operation won't allow.

Colon Notation

The colon notation builds up a vector of regularly spaced scalars:
>> x = 1:5

x = 

   1    2    3    4    5

>>
You don't have to start with 1: try "x = -7:12". Equally interesting, you don't have to use integers:
>> x = 0.1:5

x =

    0.1000    1.1000    2.1000    3.1000    4.1000

>>
Note that by default the colon notation returns a row vector. But you know how to change that into a column vector, right?

Striding Through

The colon notation allows using a non-unit stride as well. The general form is begin:stride:end, where the begin and end values are on the left and right, resp., and the stride is in the middle. Here's a way to generate a vector of positive odd integers:
>> x = 1:2:8

x =

     1     3     5     7

>>
The stride need not be positive, or even integer-valued:
>> x = 5:-0.4:1

x =

  Columns 1 through 7 

    5.0000    4.6000    4.2000    3.8000    3.4000    3.0000    2.6000

  Columns 8 through 11 

    2.2000    1.8000    1.4000    1.0000

>> 
Notice that when Matlab has to split a long row vector across lines, it gives you the column numbers above each line. Now note what happens when you try:
>> x = 1:-1:5

x =

   Empty matrix: 1-by-0

>>
Empty matrices are both useful and irritating in Matlab. When you program a loop in most languages with a positive increment and upper bound smaller than the lower bound, the loop is skipped. With Matlab colon notation, you get something: empty, but still with a well-defined size.

Most of these indexing examples seem stupid, and generally they are. Where you will end up using the indexing is where the start:stride:last triplet comes from program parameters, e.g.,

x(k:lda:n)
(and yes, that is a real example from a code that does something other than stupid Matlab tricks.)

Matrix Indexing

Colon-built vectors are handy for plotting vectors, to use as ordinates. The most common use is to index into arrays, however. Suppose the matrix A is nxn with n = 6:
  A = [ 11  12  13  14  15  16
        21  22  23  24  25  26
        31  32  33  34  35  36
        41  42  43  44  45  46
        51  52  53  54  55  56
        61  62  63  64  65  66];
Then Try coding the last operation quickly in C or Fortran! The script m-file indexing.m creates an 8x8 array and then gives some more indexing examples. You can use just the first part to set up a matrix B, and then try your own indexing on it. E.g, you should try to index to create a vector consisting of the numbers in the last column, or to create a matrix that has all entries with an even number as row index and an odd number as the column index.

The last "reordering of a matrix" method is a Matlab idiom, a commonly used command which is peculiar to the language. In comparison a common C/C++ idiom is "k++", the increment operator which replaces "k = k + 1". Matlab idioms not only let you type things in fewer keystrokes, they also will let you express operations in a manner that Matlab can make run faster. Other common idioms include

A final warning for those who know Fortran9x: the array notation is like Fortran90, but BEWARE: Matlab's colon notation has begin:stride:end, while Fortran9x notation is begin:end:stride.


  • Next page: Matlab Advice
  • Class home page: A321

  • Initialized: 12-Jan-2009
  • Modified 21-Jan-2009 11:56:05 AM , to rephrase the indexing.m file explanation.