function med = MedVal(C)
% Return the median value in the 2D array C.

% Assemble C's entries into a 1-dim array and sort
[p,q] = size(C);
n = p*q;
v = C(1:n);     % Can access 2D-array with 1D subscripts
v = sort(v);

% Median choice depends on whether n is odd or even
m = ceil(n/2);
if rem(n,2)==1
    med = v(m);
else
    med = sum([v(m), v(m+1)])/2;
end