(defun median (v1 v2) (let ((vhat)) (dotimes (i (length v1)) (if (numberp (nth i v1)) (push (/ (+ (nth i v1) (nth i v2)) 2) vhat) (if (eql (nth i v1) (nth i v2)) (push (nth i v1) vhat) (push (random-element (list (nth i v1) (nth i v2))) vhat)))) (reverse vhat))) ;(defun median (v1 v2) ; (let ((vhat)) ; (dotimes (i (length v1)) ; (if (eql (nth i v1) (nth i v2)) ; (push (nth i v1) vhat) ; (push (random-element (list (nth i v1) (nth i v2))) vhat))) ; (reverse vhat))) (defun favored-median-num (v1 v2 &optional (w1 1) (w2 1)) "v1 is weighted more than v2 in the median" (let ((vhat)) (dotimes (i (length v1)) (let (l) (dotimes (j (1+ w1)) (push (nth i v1) l)) (dotimes (k (1+ w2)) (push (nth i v2) l)) (if (numberp (nth i v1)) (push (/ (sum l) (length l)) vhat) (if (eql (nth i v1) (nth i v2)) (push (nth i v1) vhat) (push (random-element l) vhat))))) (reverse vhat))) (defun favored-median (v1 v2 &optional (w1 1) (w2 1)) "v1 is weighted more than v2 in the median" (let ((vhat)) (dotimes (i (length v1)) (let (l) (dotimes (j (1+ w1)) (push (nth i v1) l)) (dotimes (k (1+ w2)) (push (nth i v2) l)) (if (eql (nth i v1) (nth i v2)) (push (nth i v1) vhat) (push (random-element l) vhat)))) (reverse vhat))) (deftest test-median () "Return a median of the values in a table" (let ((egsx (mapcar #'eg-features (egs (weather-numerics))))) (median (random-element egsx) (random-element egsx)))) (defun sum (l) (let ((s 0)) (dolist (v l) (setf s (+ s v))) s))