Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help >  programs >  matlab

Matlab - higher dimensions

[Introduction] [Creating N-D arrays] [Manipulating N-D arrays] [Processing N-D arrays] [An example]

Introduction

Matlab supports the use of 1D vectors and 2D matrices. It also copes with higher dimensioned matrices ("N-D arrays"), though there are a few surprises. Just as a vector with 4 elements is often described as a 4x1, so a 4x3 matrix could be described as a 4x3x1 (a slice of a 4x3xn block) or even a 4x3x1x1. These single-thickness dimensions are called "singleton dimensions".

The ndims command returns how many dimensions a matrix has. It never returns less than 2. After

   m=ones(4,1)
ndims(m) return 2. After
   m=ones(4,2,3)
(which creates a 4x2x3 matrix full of 1s) ndims(m) returns 3, though after
   m=ones(4,1,1)
ndims(m) returns 2 because it ignores trailing singleton dimensions. If you create m in the following way
   m=ones(1,1,4)
m is essentially the same shape as before, but this time ndims(m) returns 3. You can do
   m=squeeze(m) 
which removes singleton dimensions after which ndims(m) returns 2.

Another complication is that

   m=ones(4,0)
is legal - it means that m is empty along one dimension.

Creating N-D arrays

We've already seen how ones can be used to create N-D arrays. zeros and randn can be used in the same way. Also

Manipulating N-D arrays

There are some new commands to manipulate N-D arrays. Often they're generalisations of commands to deal with 2-D arrays.

Changing the shape

reshape works with N-D arrays. Also

Changing the contents

Taking slices

If you want to take 2-D slices from a 3-D matrix A you can use something like A(:,:,1).

Processing N-D arrays

Some old commands have been rewritten to cope with N-D arrays. For example, sum by default sums along the first non-singleton dimension. You can make it sum along other dimensions. So
   A=randn(3,4,2)
   sum(A,3)
sums along the 3rd dimension (i.e. "depth") giving 12 values.

Note that you can't have sparse N-D arrays.

An example

Suppose you wanted to calculate the possible outcomes of rolling 2 dice (a red one and a green one, say). You could use
   for red=1:6
      for green=1:6
         outcomes(red,green)=red+green;
      end
   end
To add another (blue) die, you could do
   oldoutcomes=outcomes;
   for blue=1:6
      outcomes(:,:,blue)=oldoutcomes(:,:)+blue;
   end
We can now try some simple commands just to check that all is as expected
   ndims(outcomes)  
   size(outcomes)
   numel(outcomes)
   min (outcomes(:))
   max (outcomes(:))
   lessthansix=outcomes<6
   sum(lessthansix(:))/numel(outcomes) % the chance of getting <6
   % Now find the dice values corresponding to these throws.
   % Note that for N-D arrays you need to use ind2sub as well
   lessthansixindices=find(outcomes<6)
   [red,green,blue]=ind2sub(size(outcomes),lessthansixindices)
   % Plot them
   scatter3(red,green,blue)
See also Mathworks' Multidimensional Arrays page.
© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: August 2007