1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 1999 - INRIA - Carlos Klimann
3 // Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
4 // Copyright (C) 2016 - Samuel GOUGEON
6 // Copyright (C) 2012 - 2016 - Scilab Enterprises
8 // This file is hereby licensed under the terms of the GNU GPL v2.0,
9 // pursuant to article 5.3.4 of the CeCILL v.2.1.
10 // This file was originally licensed under the terms of the CeCILL v2.1,
11 // and continues to be available under such terms.
12 // For more information, see the COPYING file which you should have received
13 // along with this program.
16 function y = median(x,orient)
19 // - modified by farid.belahcene:the case when x is an hypermatrix
20 // - new syntaxes: median(x,'m') and median(x,dim)
22 // - Fixes overflow issues with encoded integers when interpolating
23 // http://bugzilla.scilab.org/14640
30 msg = _("%s: Wrong number of input argument(s): %d or %d expected.\n")
31 error(msprintf(msg, "median", 1, 2))
39 elseif orient=="c" then
41 elseif orient=="*" then
43 elseif orient=="m" then
44 orient = find(size(x)>1,1)
49 if type(orient)<>1|size(orient,"*")<>1|~isreal(orient)|orient<=0 then
50 msg = _("%s: Wrong value for input argument #%d: ''%s'', ''%s'',''%s'' or a positive number expected.\n")
51 error(msprintf(msg, "median",2,"r","c","m"))
58 // median on all components
59 // ------------------------
66 x = gsort(x(:),"g","i")
68 // avoid overflow: http://bugzilla.scilab.org/14640
71 y = a/2 + b/2 + ((a-(a/2)*2) + (b-(b/2)*2))/2
76 // Projection along a given direction / dimension
77 // ----------------------------------------------
83 if orient>ndims(x) then
88 if xsize(orient)==1 then
92 orient_above_size = xsize(orient+1:$);
93 N = prod(orient_above_size)
94 orient_below_size = xsize(1:orient-1);
95 M = prod(orient_below_size)
96 orient_size = xsize(1:orient);
102 ytemp = gsort(x(i+(0:n-1)*M+(k-1)*P),"r","i")
103 if 2*int(n/2)==n then
104 // avoid overflow: http://bugzilla.scilab.org/14640
107 y = [ y ; a/2 + b/2 + ((a-(a/2)*2) + (b-(b/2)*2))/2]
109 y = [ y ; ytemp((n+1)/2)]