<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/cts.median.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>cts.median</api:function-name><api:suggest>cts.median</api:suggest><api:suggest>cts</api:suggest><api:suggest>median</api:suggest><api:function-link mode="xquery" fullname="cts:median">/apidoc/8.0/cts:median.xml</api:function-link><api:function mode="javascript" name="median" type="builtin" lib="cts" category="SearchBuiltins" subcategory="Math Lexicon" hidden="false" bucket="MarkLogic Built-In Functions" prefix="cts" namespace="http://marklogic.com/cts" fullname="cts.median"><api:summary>
  Returns a frequency-weighted median of a sequence.
  This function works like <code xmlns="http://www.w3.org/1999/xhtml">math:median</code> except each item in the
  sequence is repeated <code xmlns="http://www.w3.org/1999/xhtml">cts:frequency</code> times before calculating
  the median. If $arg is the empty sequence, the function returns the empty
  sequence.
</api:summary><api:params><api:param name="arg" type="xs:double*"><api:param-description>
   The sequence of values. The values should be the result of a lexicon lookup.
  </api:param-description><api:param-name>arg</api:param-name><api:param-type>Number[]</api:param-type></api:param></api:params><api:return>Number?</api:return><api:usage>
 <p xmlns="http://www.w3.org/1999/xhtml">This function is designed to take a sequence of values returned
 by a lexicon function (for example, <code>cts:element-values</code>); if you
 input non-lexicon values, the result will be the empty sequence.</p>
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//  This query assumes an element range index of type 'int'
//  is configured for the 'int' element in the database. It
//  generates some sample data and then performs the aggregation
//  in a separate transaction.

declareUpdate();

for (x=1; x&lt;11; x++) {
  var o = new Object();
  o.int = [];
  for (y=x; y&lt;11; y++) { 
    o.int.push(y);    
  };
  xdmp.documentInsert((x + ".json"), o); 
};

******
// Run the following query:

var res = new Array();
res.push(cts.median(cts.values(
  cts.jsonPropertyReference("int", "type=int"), null,
                            ["item-frequency"])));
res.push(cts.median(cts.values(
  cts.jsonPropertyReference("int", "type=int"), null,
                            ["fragment-frequency"])));
res;
=&gt;
[7, 7]


</pre></api:example></api:function></api:function-page>