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
  [p,q] = size(C);
  x = [];
  for k=1:p
     x = [x C(k,:)];
  end
  x = sort(x);
  
% x has n elements
  n = p*q;
  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