Created
November 9, 2011 22:12
-
-
Save mwmitchell/1353271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
# groups an array of sorted numbers | |
# into clusters, where each cluster contains | |
# values that are more close in value than | |
# either the left or right neighboring clusters. | |
def clusterize | |
c = [[]] | |
each_with_index do |x,i| | |
# first time 'round, add the value (x) | |
# to the current "cluster", then move | |
# to the next value | |
c[-1] << x && next if i == 0 | |
# n is the difference of the next value | |
# and the current value.... | |
# or 0 if there isn't a next | |
n = i == size-1 ? 0 : x - self[i+1] | |
# p is the diff. of the next value | |
# and the current value | |
p = self[i-1] - x | |
# add a new "cluster"... | |
# if the previous diff is greater than the next diff | |
c << [] if p > n | |
# add the current value (x) to the last cluster | |
c[-1] << x | |
end | |
c | |
end | |
end | |
data = [9.537862, 9.537862, 6.579175, 3.3948307, 3.3948307, 3.2919545, 3.1890779, 3.1330175, 3.0862014, 3.0862014, 3.0862014, 3.0862014, 3.0862014, 3.0862014, 2.731317, 2.731317, 2.2361403, 2.2361403, 2.174609, 2.1746087, 1.763103, 1.7116647, 1.6579969, 1.6579969, 1.6579969, 1.6395404, 1.6395404, 1.6395404, 1.5465399, 1.4947798] | |
result = data.clusterize | |
# result => | |
[[9.537862, 9.537862, 6.579175], [3.3948307, 3.3948307, 3.2919545], [3.1890779], [3.1330175], [3.0862014, 3.0862014, 3.0862014, 3.0862014, 3.0862014, 3.0862014], [2.731317, 2.731317], [2.2361403, 2.2361403], [2.174609, 2.1746087], [1.763103, 1.7116647], [1.6579969, 1.6579969, 1.6579969], [1.6395404, 1.6395404, 1.6395404], [1.5465399], [1.4947798]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment