Skip to content

Instantly share code, notes, and snippets.

@pingles
Created May 8, 2011 09:34
Show Gist options
  • Save pingles/961257 to your computer and use it in GitHub Desktop.
Save pingles/961257 to your computer and use it in GitHub Desktop.
(matrix (repeat 10 (range 1 4)))
; [1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000
; 1.0000 2.0000 3.0000]
(defn normalise
"Normalises columns 0..n-1"
[data]
(let [[rows cols] (dim data)
col-range (range 0 (- cols 1))
mins (trans (concat (map (fn [i]
(let [v (apply min (sel data :cols i))]
(repeat rows v)))
col-range)
(list (repeat rows 0))))
ranges (trans (concat (map (fn [i]
(let [rmax (apply max (sel data :cols i))
rmin (apply min (sel data :cols i))]
(repeat rows (- rmax
rmin))))
col-range)
(list (repeat rows 1))))]
(div (minus data mins) ranges)))
(normalise (matrix [[1 200 1 0]
[0 0 100 0]
[2 100 1 0]
[100 1 0 2]]))
; [0.0100 1.0000 0.0100 0.0000
; 0.0000 0.0000 1.0000 0.0000
; 0.0200 0.5000 0.0100 0.0000
; 1.0000 0.0050 0.0000 2.0000]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment