code: def coarsen_matrix(Z, xlevel=0, ylevel=0, method='average'):
      
  
      # coarsen x
      if not ylevel:
          Z_coarsened = Z
      else:
          temp = []
          for z in Z: temp.append(coarsen_array(z, ylevel, method))
          Z_coarsened = _n.array(temp)
  
      # coarsen y
      if xlevel:
          Z_coarsened = Z_coarsened.transpose()
          temp = []
          for z in Z_coarsened: temp.append(coarsen_array(z, xlevel, method))
          Z_coarsened = _n.array(temp).transpose()
  
      return Z_coarsened
  
  
  
      # first coarsen the columns (if necessary)
      if ylevel:
          Z_ycoarsened = []
          for c in Z: Z_ycoarsened.append(coarsen_array(c, ylevel, method))
          Z_ycoarsened = _n.array(Z_ycoarsened)
  
      # now coarsen the rows
      if xlevel: return coarsen_array(Z_ycoarsened, xlevel, method)
      else:      return _n.array(Z_ycoarsened)
docs: This returns a coarsened numpy matrix.
  
      method can be 'average', 'maximum', or 'minimum'