Centroid of a set of N-dimensional points: Difference between revisions

Added Wren
m (→‎{{header|Julia}}: fix error handling)
(Added Wren)
Line 81:
</pre>
[[File:Plot centroid.png|center|thumb]]
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascript">var centroid = Fn.new { |pts|
var n = pts.count
var d = pts[0].count
var res = List.filled(d, 0)
for (j in 0...d) {
for (i in 0...n) {
res[j] = res[j] + pts[i][j]
}
res[j] = res[j] / n
}
return res
}
 
var points = [
[ [1], [2], [3] ],
[ [8, 2], [0, 0] ],
[ [5, 5, 0], [10, 10, 0] ],
[ [1, 3.1, 6.5], [-2, -5, 3.4], [-7, -4, 9], [2, 0, 3] ],
[ [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0] ]
]
 
for (pts in points) {
System.print("%(pts) => Centroid: %(centroid.call(pts))")
} </syntaxhighlight>
 
{{out}}
<pre>
[[1], [2], [3]] => Centroid: [2]
[[8, 2], [0, 0]] => Centroid: [4, 1]
[[5, 5, 0], [10, 10, 0]] => Centroid: [7.5, 7.5, 0]
[[1, 3.1, 6.5], [-2, -5, 3.4], [-7, -4, 9], [2, 0, 3]] => Centroid: [-1.5, -1.475, 5.475]
[[0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0]] => Centroid: [0, 0.25, 0.25, 0.25, 0.25]
</pre>
[[File:wren-centroid.png|center|thumb]]
9,483

edits