     result = mat[0][0]
     for i of mat
         for j of mat[i]
-            result = mat[i][j] if mat[i][j]? and result > mat[i][j]
+            result = mat[i][j] if !(result?) or (result > mat[i][j] and mat[i][j]?)
     result
 
 matrixMax = (mat) ->
     result = mat[0][0]
     for i of mat
         for j of mat[i]
-            result = mat[i][j] if mat[i][j]? and result < mat[i][j]
+            result = mat[i][j] if !(result?) or (result < mat[i][j] and mat[i][j]?)
     result
 
 matrixMaxAbs = (mat) ->
     result = Math.abs(mat[0][0])
     for i of mat
         for j of mat[i]
-            result = Math.abs(mat[i][j]) if mat[i][j]? and result < Math.abs(mat[i][j])
+            result = Math.abs(mat[i][j]) if !(result?) or (result < Math.abs(mat[i][j]) and mat[i][j]?)
     result
 
 matrixExtent = (mat) -> [matrixMin(mat), matrixMax(mat)]

pullVarAsArray = (data, variable) ->
    (data[i][variable] for i of data)

# Select a set of categorical colors
# ngroup is positive integer
# palette = "dark" or "pastel"

        return x[(n-1)/2]
    (x[n/2] + x[(n/2)-1])/2

# given a vector of x's, return hash with values to left and right, and the differences
getLeftRight = (x) ->
    n = x.length
    x.sort( (a,b) -> a-b )

    xdif = []
    result = {}
    for v in x
        result[v] = {}

    for i in [1...n]
        xdif.push(x[i]-x[i-1])
        result[x[i]].left = x[i-1]
    for i in [0...(n-1)]
        result[x[i]].right = x[i+1]

    xdif = median(xdif)
    result.mediandiff = xdif

    result[x[0]].left = x[0]-xdif
    result[x[n-1]].right = x[n-1]+xdif
    result.extent = [x[0]-xdif/2, x[n-1]+xdif/2]

    result

# maximum difference between adjacent values in a vector
maxdiff = (x) ->
    return null if x.length < 2
