Averages/Median: Difference between revisions

(corrected categorization of task)
Line 143:
let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];;
median a;;</lang>
 
=={{header|Octave}}==
Of course Octave has its own <tt>median</tt> function we can use to check our implementation. The Octave's median function, however, does not handle the case you pass in a void vector.
<lang octave>function y = median2(v)
if (numel(v) < 1)
y = NA;
else
sv = sort(v);
l = numel(v);
if ( mod(l, 2) == 0 )
y = (sv(floor(l/2)+1) + sv(floor(l/2)))/2;
else
y = sv(floor(l/2)+1);
endif
endif
endfunction
 
a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2];
b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2];
 
disp(median2(a)) % 4.4
disp(median(a))
disp(median2(b)) % 4.25
disp(median(b))</lang>
 
=={{header|Python}}==