function med = medVal(C)
% C is a p-by-q array
% med is the median of the values in C

% Assemble C's entries into a 1-dim array and sort
  [nr,nc] = size(C);
  x = zeros(1,nr*nc);
  for r=1:nr
     x((r-1)*nc+1:r*nc) = C(r,:);
  end
  x = sort(x);
  
% x has n elements
  n = nr*nc;
  m = ceil(n/2);
  
% Median choice depends on whether n is odd or even
  if rem(n,2)==1
    med = x(m);
  else
    med = x(m)/2 + x(m+1)/2;
  end