If you have trouble viewing this, try the pdf of this post. You can download the code used to produce the figures in this post.

One of my Matlab programming styles, some would say quirks, is wide use of complex variables. I not only use them in the standard mathematical places but I use them to represent two dimensional spatial vectors and to represent two dimensional quantities in general.

Using complex numbers to represent two dimensional vectors is straightforward, a complex number is *x* + *iy*. OK, so why do it? First, Matlab handles complex numbers seamlessly. So, adding two complex numbers is a vector addition. All the standard data structures such as vectors, matrices, and multi-dimensional arrays can use complex numbers. Many functions such as *linspace* and *plot* handle complex numbers. You do need to be aware of some quirks. The transpose operator ’ also does a complex conjugate. If this is not what you want, use .’.

What about the standard vector products? For these you have to remember some formulas

To help me to use complex numbers I wrote the *zcomp* function. This is a two way function to convert from real to complex and vice-versa. This avoids having to use *complex*() and [*real*() *imag*()]. Some examples:

My PolarTransform.m function is an example of the use of complex numbers as coordinates of an image. The function extracts a pie-shaped sector from an image. Fig. 1↓ shows how it is used. The left panel shows the original image, the center shows a subset of the sample points, and the left panel shows the sector image. It is flipped so the sector center is below the bottom.

The code for the function is below. It shows how complex numbers as vectors allow compact and readable code.

Another useful utility function is *zarrow*. This draws an arrow on an image from start and end points in image coordinates. It uses the Matlab *annotation* function, which is a misbegotten creation that specifies the arrow endpoints in normalized figure units with the x’s and y’s specified separately! *zarrow* uses complex vectors to specify the arrow more naturally in image coordinates. Fig 2↓ shows how to draw an arrow to the center of the pie-shaped sector in the previous example.

A final example exploits Matlab’s mysterious ability to handle integers and floating point numbers with the same variable. In some of my work I have to deal with patient IDs. These are anonymized to protect confidentiality and consist of a Series ID, which is a letter, and a patient number. Dealing with these is a pain since it is easy to interchange them so I represent the complete ID as a complex number *zpatid*, where the real part is the ASCII code of the Series ID and the imaginary part is the patient number. Even though complex numbers are double’s Matlab has no problem testing two *zpatid*’*s* for equality.

We also need better ways to sort complex numbers. The Matlab sort function results in a weird ordering by the magnitude of the complex numbers. I wrote the *zsort* function that gives more options for sorting. The default is a lexical sort that sorts first by the real part then by the imaginary.

You can download the code for all the functions mentioned here.

I will have more to say later on using complex variables including functions to implement a statistical shape model for Active Shape Model image segmenting[1].

Last edited Aug. 9, 2011

© 2011 by Aprend Technology and Robert E. Alvarez

Linking is allowed but reposting or mirroring is expressly forbidden.

[1] : “Active shape models - their training and application”, *Computer Vision and Image Understanding*, pp. 38—59, 1995.