Writing Fast Matlab Code #7: Referencing Operations

November 9, 2009 by Pascal Getreuer 
Filed under: Code Optimization, Tutorials 
Leave a Comment
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)

Writing Fast Matlab code

Referencing in Matlab is varied  and powerful enough to deserve a section of discussion.  Good under- standing of referencing  enables vectorizing  a broader  range of programming situations.

Subscripts vs.  Indices

Subscripts  are the  most  common method  used to refer to matrix  elements,  for example,  A(3,9) refers to  row  3,  column  9.   Indices  are  an  alternative referencing  method.    Consider  a  10 × 10 matrix   A. Internally, Matlab stores  the matrix  data  linearly  as a one-dimensional,  100-element array  of data  in column-major order.

1 11 21 · · · 81 91
2 12 22 82 92
3 13 23

83 93
.

.

.

.

.
10 20 30 · · · 90 100

An index refers to an element’s position in this one-dimensional  array.  Using an index, A(83) also refers to the element on row 3, column 9.

It is  faster to scan down columns than over rows. Column-major order  means  that elements along a column are sequential  in memory while elements  along a row are further  apart. Scanning down columns promotes  cache efficiency.

Conversion  between  subscripts and  indices  can  be  done  with  the  sub2ind and  ind2sub functions. However, because  these  are M-file functions  rather than  fast built-in  operations, it is more efficient to compute  conversions  directly  (also  see Section  6).   For  a two-dimensional  matrix  A of size M×N, the conversions  between  subscript (i,j) and index (index) are

A(i,j)  ↔   A(i + (j-1)*M)
 
A(index) ↔   A(rem(index-1,M)+1, floor((index-1)/M)+1)

Indexing  extends  to  N-D  matrices   as  well,  with  indices  increasing  first  through the  columns,  then through the rows, through the third  dimension,  and so on.  Subscript notation extends  intuitively,

A(. . . , dim4, dim3, row, col).

Vectorized Subscripts

It  is useful to  work with  submatrices rather than  individual  elements.   This  is done with  a vector  of indices or subscripts. If A is a two-dimensional matrix,  a vector  subscript reference has the syntax

A(rowv, colv)

where rowv and colv are vectors.  Both  may be of any length  and their  elements  may be in any order. If either  is a matrix, it  is reshaped  to  a vector.   There  is no difference between  using row vectors  or column vectors  in vector  subscripts.

Let M = length(rowv) and  N = length(colv).  Then  a vector  subscripted matrix  on the  right-hand side∗  returns a submatrix of size M×N:

FastMatlabcode7_1

A vector  subscripted matrix  can also appear  on the left-hand  side as

A(rowv, colv) = scalar    or    M×N matrix

If any  elements  in the  destination reference  are  repeated, the  source  element  with  the  greater  index dominates. For example,

>> A  =  zeros(2); A([1,2],[2,2]) =  [1,2;3,4]

A  =
0           2
0           4

Vector  subscript references extend  intuitively in higher dimensions.


∗ The  left-  and  right-hand sides  (LHS  and  RHS)  refer  to  the  two sides  of an  assignment, “LHS = RHS.”

Vector Indices

Multiple  elements  can also be referenced  with vector  indices.

A(indexv)

where indexv is an array  of indices.  On the right-hand side, a vector  index reference returns a matrix the same size and shape as indexv.  If indexv is a 3 × 4 matrix, then  A(indexv) is the 3 × 4 matrix

FastMatlabcode7_2

While vector subscripts are limited  to referring to block-shaped  submatrices, vector indices can refer to any subset  of elements.

If a vector index reference is on the left-hand  side, the right-hand side must return a matrix  of the same size as indexv or a scalar.  As with  vector  subscripts, ambiguous  duplicate  assignments use the  value with greater  source index.

Reference Wildcards

Using the  wildcard,  :, in a subscript refers to  an  entire  row or column.   For  example,  A(:,1) refers to  every  row in column  one–the  entire  first  column.   This  can  be combined  with  vector  subscripts, A([2,4],:) refers to the second and fourth  rows.

When  the  wildcard  is used in a vector  index,  the  entire  matrix  is referenced.  On the  right-hand side, this always returns a column vector.

A(:) = column vector

This is frequently  useful: for example,  if a function  input  must  be a row vector,  the user’s input  can be quickly reshaped  into row vector  with A(:).’ (make  a column vector  and transpose to a row vector).

A(:) on the  left-hand  side assigns  all the  elements  of A, but  does not  change  its  size.  For  example,

A(:) = 8 changes all elements  of matrix  A to 8.

Logical Indexing

Given a logical array  mask with the same size as A,

A(mask)

refers  to  all elements  of A where  the  corresponding  element  of mask is 1 (true).  It  is equivalent  to A(find(mask)).  A common  usage of logical indexing  is to  refer to  elements  based  on a per-element condition,  for example,

A(abs(A) < 1e−3)  =  0

sets all elements  with  magnitude less than  10−3  to zero.  Logical indexing  is also useful to select non- rectangular sets of elements,  for example,

A(logical(eye(size(A))))

references  the  diagonal  elements  of  A. Note  that for a  right-hand side  reference,  it  is faster  to  use

diag(A) to get the diagonal  of A.

Deleting Submatrices with [ ]

Elements  in a matrix  can  be deleted  by  assigning  the  empty  matrix.  For  example,  A([3,5]) = [ ] deletes the third  and fifth element from A. If this is done with index references, the matrix  is reshaped into a row vector.

It  is also possible to  delete  with  subscripts if all but  one subscript  are  the  wildcard.

A(2,:)   = [ ]

deletes the second row.

Deletions  like A(2,1) = [ ] or even A(2,1:end) = [ ] are illegal.

Download Full Pdf Version

VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

Comments

Comments are closed.