Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2D Gaussian function in Scala
import scala.math._
def gauss(dim: Int, sigma: Float): Array[Array[Float]] = {
if(dim%2==0)
throw new Exception("dim must be odd.")
val arr = Array.fromFunction(i=>{
Array.fromFunction({j=>
val x = (i - (dim-1)/2).toFloat
val y = (j - (dim-1)/2).toFloat
(exp(-1 * pow(x/sigma,2)) * exp(-1 * pow(y/sigma,2))).toFloat
})(dim)})(dim)
val ret = arr.map(a=>a.map(b=>b/sum(arr)))
ret
}
def sum(arr: Array[Array[Float]]): Float = {
arr.map(row=>row.sum).sum
}
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.