Techniques used in MEDIAN

Arrays, lists, and stack elements. MEDIAN extracts a column of data from ΣDAT in vector form. To convert the vector to a list, MEDIAN puts the vector elements on the stack and combines them into a list. From this list the median is calculated using %TILE.

The median for the mth column is calculated first, and the median for the first column is calculated last. As each median is calculated, ROLLD is used to move it to the top of the stack.

After all medians are calculated and positioned on the stack, they’re combined into a vector.

FOR…NEXT (definite loop with counter). MEDIAN uses a loop to calculate the median of each column. Because the medians are calculated in reverse order (last column first), the counter is used to reverse the order of the medians.

Required Program

%TILE (page 2-10) sorts a list and returns the value of a specified percentile.

MEDIAN program listing (Note: Use approximate mode for this program and example).

Program:

Comments:

 

 

«

 

RCLΣ

Puts a copy of the current statistics matrix ΣDAT on

 

the stack.

DUP SIZE

Puts the list { n m } on the stack, where n is the

 

number of rows in ΣDAT and m is the number of

 

columns.

OBJ→ DROP

Puts n and m on the stack, and drops the list size.

→ s n m

Creates local variables for s, n, and m.

«

Begins the defining procedure.

'ΣDAT' TRN

Recalls and transposes ΣDAT.

 

Now n is the number of columns in ΣDAT and m is

 

the number of rows. (To key in the Σ character, press

 

, then delete the parentheses.)

1 m

Specifies the first and last rows.

FOR j

For each row, does the following: Extracts the last row

Σ-

in ΣDAT.

 

Initially this is the mth row, which corresponds to the

 

mth column in the original ΣDAT.

 

(To key in the Σcommand, use .)

OBJ→ DROP

Puts the row elements on the stack. Drops the index

 

list { n }.

n →LIST

Makes an n-element list.

50 %TILE

Sorts the list and calculates its median.

j ROLLD

Moves the median to the proper stack level.

NEXT

Increments j and repeats the loop.

 

 

2-12 RPL Programming Examples