def matrix_elem(matrix, i, j, val=None):
  """
  Reads/writes the elements of an affine transform.

  1. 3x3 rotational component;
      matrix_elem(m, i, j) for i=0..3, j=0..3
  2. 3x1 translational component:
      matrix_elem(m, 3, i) for i=0..3)
  """
  if val is None:
    return matrix[j,i]
  else:
    matrix[j,i] = val