Skip to content

Instantly share code, notes, and snippets.

@knugie knugie/vector_cluster.rb
Last active Jun 29, 2016

Embed
What would you like to do?
Cluster vectors by their similarity defined by a tolerance vector
require 'matrix'
tolerance = Vector[5, 5, 5]
elements = [Vector[2, 1, 4], Vector[20, 100, 25], Vector[21, 98, 21], Vector[1, 2, 3]]
clusters = []
while(elements.any?)
element = elements.pop
cluster, elements = elements.partition do |elem|
diff = tolerance - (elem - element).map(&:abs)
diff.all? { |s| s > 0 }
end
clusters << (cluster << element)
end
clusters.each { |cluster| p cluster }
# [Vector[2, 1, 4], Vector[1, 2, 3]]
# [Vector[20, 100, 25], Vector[21, 98, 21]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.