median()

Read(702) Label: sequence, logical division,

Here’s how to use median() function.

A.median( k:n )

Description:

Divide a sequence into multiple segments and return the specified bordering member(s).

Syntax:

A.median(k:n)

Divide sequence A into n segments evenly according to its length and return the bordering member between the kth segment and the (k+1)th segment

A.median(k:n,x)

Calculate expression x over each member of sequence A to generate a new sequence that will be ordered automatically, then divide the new sequence into n segments evenly according to its length and return the bordering member between the kth segment and the (k+1)th segment

Note:

The function returns a sequence of the bordering members between every two segments when parmeter k is absent but parameter n is present.

When parameter x is present, prameters k,n should be always in place. When prameters k,n are absent, if the length of the sequence is an odd, return the median member value; if it is an even, return the average value of the two members in the middle. Perform a logical division if the length of the sequence is indivisible.

Parameter:

A

A sequence

k

The kth segment (1=<k<=n); return the bordering members of between every two segments as sequence when k is absent

n

The number of to-be-divided segments, which is greater than 1 and whose default is 2; can be omitted when parmeter k is absent

x

An expression

Return value:

A number or a sequence

Example:

 

A

 

1

=[1,2,3,4,5,6,7,8]

 

2

=A1.median()

As all parameters are absent and the length of the sequence is an even number, the function returns the average of the two members in the middle, that is (4+5)/2=4.5

3

=A1.median(2:3)

The sequence is divided as follows:

Divide the sequence into 3 segments and return the bordering member between the second segment and the third on, which is 6

4

=A1.median(:3)

As no segment is specified, return the bordering members between every two segments as a sequence, which is [3,6]

5

=A1.median(:10)

The sequence is divided as follows:

Result: [1,2,3,4,4.5,5,6,7,8]

6

=A1.median(:,~%2)

Divide each member of sequence A1 by 2 and get the remaider to generate a new sequence in ascending order: [0,0,0,0,1,1,1,1]; As prameters k,n are absent and the the length of the sequence is an even, the function returns the average value of the two members in the middle: (0+1)/2=0.5

7

A1.median(2:3,~%2)

Divide the new sequence evenly into 3 segments and return the bordering member between the 2nd and the 3rd : 1