Centroid of a set of N-dimensional points: Difference between revisions
Content added Content deleted
(→{{header|Perl}}: Added Perl solution and graph) |
(Added XPL0 example.) |
||
Line 339: | Line 339: | ||
[[File:wren-centroid.png|center|thumb]] |
[[File:wren-centroid.png|center|thumb]] |
||
=={{header|XPL0}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang "XPL0">include xpllib; \for Print |
|||
proc Centroid(N, D, Pts); |
|||
int N, D; real Pts; |
|||
int I, J; |
|||
real Ctr; |
|||
[Ctr:= RlRes(D); |
|||
for J:= 0 to D-1 do |
|||
[Ctr(J):= 0.0; |
|||
for I:= 0 to N-1 do |
|||
Ctr(J):= Ctr(J) + Pts(I,J); |
|||
Ctr(J):= Ctr(J) / float(N); |
|||
]; |
|||
Print("["); |
|||
for I:= 0 to N-1 do |
|||
[Print("["); |
|||
for J:= 0 to D-1 do |
|||
[Print("%g", Pts(I,J)); |
|||
if J < D-1 then Print(", "); |
|||
]; |
|||
Print("]"); |
|||
if I < N-1 then Print(", "); |
|||
]; |
|||
Print("] => Centroid: ["); |
|||
for J:= 0 to D-1 do |
|||
[Print("%g", Ctr(J)); |
|||
if J < D-1 then Print(", "); |
|||
]; |
|||
Print("]\n"); |
|||
]; |
|||
real Pts1, Pts2, Pts3, Pts4, Pts5; |
|||
[Pts1:= [ [1.], [2.], [3.] ]; |
|||
Pts2:= [ [8., 2.], [0., 0.] ]; |
|||
Pts3:= [ [5., 5., 0.], [10., 10., 0.] ]; |
|||
Pts4:= [ [1., 3.1, 6.5], [-2., -5., 3.4], [-7., -4., 9.], [2., 0., 3.] ]; |
|||
Pts5:= [ [0., 0., 0., 0., 1.], [0., 0., 0., 1., 0.], [0., 0., 1., 0., 0.], |
|||
[0., 1., 0., 0., 0.] ]; |
|||
Centroid(3, 1, Pts1); |
|||
Centroid(2, 2, Pts2); |
|||
Centroid(2, 3, Pts3); |
|||
Centroid(4, 3, Pts4); |
|||
Centroid(4, 5, Pts5); |
|||
]</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> |