Skip to content

Instantly share code, notes, and snippets.

@FlexMonkey
Created March 20, 2016 17:52
Show Gist options
  • Save FlexMonkey/8b3ab538242f3376b2c9 to your computer and use it in GitHub Desktop.
Save FlexMonkey/8b3ab538242f3376b2c9 to your computer and use it in GitHub Desktop.
Normalise a CIVector - ideal for use as a convolution matrix
extension CIVector
{
func normalize() -> CIVector
{
var sum: CGFloat = 0
for i in 0 ..< self.count
{
sum += self.valueAtIndex(i)
}
if sum == 0
{
return self
}
var normalizedValues = [CGFloat]()
for i in 0 ..< self.count
{
normalizedValues.append(self.valueAtIndex(i) / sum)
}
return CIVector(values: normalizedValues,
count: normalizedValues.count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment