14,269,076 members
Rate this:
See more:
Hello !

I want to average of each group of 20 data of Barycentre_x Barycentre_y and Barycentre_z. These variables have a number of "cpt" lines (around 600 lines).

I try to do this, but it doesn't run.

Barycentre_x = new double[cpt - 1];
Barycentre_y = new double[cpt - 1];
Barycentre_z = new double[cpt - 1];

for (int i = 0; i < cpt-1; i++) {
for (int l = 55; l < 63; l++) {

Barycentre_x [i] += nv_data [l].positions [i].x;
Barycentre_y [i] += nv_data [l].positions [i].y;
Barycentre_z [i] += nv_data [l].positions [i].z;

}

Barycentre_x [i] /= 8;
Barycentre_y [i] /= 8;
Barycentre_z [i] /= 8;

}

// Here I begin the average

Moy_bar_x = new double[Barycentre_x.Length / 20];
Moy_bar_y = new double[Barycentre_y.Length / 20];
Moy_bar_z = new double[Barycentre_z.Length / 20];

while (index_moy + 20 < Barycentre_z.Length) {

Moy_bar_x [index_moy % 20] += Barycentre_x [index_moy];
Moy_bar_y [index_moy % 20] += Barycentre_y [index_moy];
Moy_bar_z [index_moy % 20] += Barycentre_z [index_moy];

index_moy ++;

if (index_moy % 20 == 0) {

indice_2++;

Moy_bar_x [indice_2] /= 20;
Moy_bar_y [indice_2] /= 20;
Moy_bar_z [indice_2] /= 20;
}
}

At the beginning of the code :

int index_moy = 0;
int indice_2 = -1;
Posted
Updated 14-Jun-15 22:21pm
v6

Rate this:

## Solution 1

Try this;
static void Main(string[] args)
{
const int cpt = 153;
const int taille = 20;
const double X = 21.5;
const double Y = -1.98;
const double Z = 1100.0;
double [] Barycentre_x = new double [cpt];
double [] Barycentre_y = new double [cpt];
double [] Barycentre_z = new double [cpt];

Random rand = new Random();
for (int n=0; n < cpt; ++n)
{
Barycentre_x[n] = X + (rand.NextDouble()-.5) * Math.Abs(X)/100.0;
Barycentre_y[n] = Y + (rand.NextDouble()-.5) * Math.Abs(Y)/100.0;
Barycentre_z[n] = Z + (rand.NextDouble()-.5) * Math.Abs(Z)/100.0;
}

const int groupes = cpt / taille;

double [] Moy_bar_x = new double[groupes];
double [] Moy_bar_y = new double[groupes];
double [] Moy_bar_z = new double[groupes];

int index = 0;
for (int g = 0; g< groupes; ++g)
{
Moy_bar_x[g] = Moy_bar_y[g] = Moy_bar_z[g]= 0.0;
for (int n=0; n<taille; ++n, ++index)
{
Moy_bar_x [g] += Barycentre_x [index];
Moy_bar_y [g] += Barycentre_y [index];
Moy_bar_z [g] += Barycentre_z [index];
}
Moy_bar_x[g] /= taille;
Moy_bar_y[g] /= taille;
Moy_bar_z[g] /= taille;

}
for (int g = 0; g < groupes; ++g)
{
Console.WriteLine("Moy_bar = ({0},{1},{2})", Moy_bar_x[g], Moy_bar_y[g], Moy_bar_z[g]);
}
}
v2
Rate this:

## Solution 2

If you want to do a moving average, then there are two ways you can do it:
1) Keep the last twenty items in an array or similar, and replace the oldest with the latest each time, then re-sum the array.
2) Keep a running total, and when you have twenty items, subtract the value at current-position-twenty from the total, and add the current value.

If you are trying to sum them in groups of twenty, then just use two loops:
Outer loop iterates groups (i.e. moves on in twenties)
Inner loop iterates within a group.

All you need is two indexes (group for output, input) and two counts.
Coralie B 15-Jun-15 4:05am

I update my question.

Is it the good way ?

But I have an error (in comment of my update question)
Coralie B 15-Jun-15 4:32am

What I do it runs. There are no error message.

But I don't average correctly...
OriginalGriff 15-Jun-15 5:02am

"run" does not mean "work".
Neither does "no error message"

Seriously, as both CPallini and I have suggested, two loops are a much better option...
Coralie B 15-Jun-15 4:59am

In my code, I do what I want ?

I calculate the average Moy_bar_ for each group of 20 data which comes from Baycentre_ ?
OriginalGriff 15-Jun-15 5:13am

Clearly not, if it isn't generating the right values?
Coralie B 15-Jun-15 5:55am

Where can I put loop ?
OriginalGriff 15-Jun-15 6:32am

Read what I said: you want two loops, one inside the other.
The outer loop processes groups, the inner loop sums values within the group.
When the inner loop completes, the outer loop sets the group average and moves on to the next group.

Exactly the same as you would if you are counting coins: you group them into "bunches of five, or ten - then count the groups. Make sense?
Coralie B 15-Jun-15 7:35am

What I do, follows your idea ?
OriginalGriff 15-Jun-15 7:48am

You know how to do a for loop, yes?
So start by putting one for loop inside another.
Coralie B 15-Jun-15 8:09am

But I don't know what I have to indicate in my for loop.
I don't know why my code doesn't work. It doesn't make the average, and it seems my code is correct. So I don't know how I can put other things
OriginalGriff 15-Jun-15 8:21am

Forget what you have got for the moment.
Start by creating (and testing) the outer (group) and inner (summing) loops: for the moment, just put a Console.WriteLine that prints the group / index inside each loop and check that it runs correctly.
Then show me what you have, OK?
Coralie B 15-Jun-15 8:51am

Barycentre_z
793,5195847
793,5012283
793,5120659
793,5141335
793,543541
793,5664139
793,5417862
793,5138206
793,485775
793,468998
793,4919357
793,5143852
793,5605545
793,5556831
793,5429459
793,5221901
793,4961166
793,5163803
793,5228043
793,544735
793,553463
793,5285034
793,5216599
793,4957657
793,4922676
793,517704
793,530529
793,5445747
793,5337791
793,5134506
793,4992294
793,475132
793,4817963
793,4707069
793,4811974
793,4586716
793,4102135
793,4006233
793,3840523
793,3606796
793,3767319
793,3855171
793,3636169
793,3158531
793,2700081
793,2549438
793,2374153
793,2268867
793,2174797
793,1958961
793,129055
793,0682297
793,0007782
792,9449158
792,8918228
792,8582954
792,8087616
792,7434807
792,6790123
792,6226845
792,5166054
792,4404259
792,4128036
792,3896904
792,3279419
792,2118835
792,1081848
791,9915619
791,8733292
791,7542686
791,6339149
791,4983063
791,3294868
791,1600456
790,9780617
790,7964935
790,6227684
790,4728012
790,2830963
790,0729904
789,8491631
789,6268272
789,378212
789,1000252
788,8318977
788,5263329
788,1875229
787,7971992
787,386116
786,9290504
786,4379959
785,8827286
785,2577171
784,6143837
783,8979073
783,1242905
782,3257523
781,481678
780,5817032
779,6189232
778,5624924
777,4394531
776,2223663
774,9666634
773,6567383
772,2775536
770,8477936
769,3564072
767,7789001
766,1640244
764,4582901
762,6655045
760,8608246
758,9834976
757,0312271
755,0118217
752,9168663
750,8171654
748,6719856
746,4749527
744,2405586
741,9915466
739,6630669
737,311924
734,9061508
732,4309807
729,933773
727,4210587
724,8427353
722,2602806
719,6165466
716,9647369
714,3009987
711,6264992
708,9709167
706,2721405
703,551609
700,7910538
697,9576111
695,108017
692,241291
689,3961334
686,5336571
683,6578331
680,7682686
677,8187256
674,8672218
671,8858528
668,8981628
665,9049377
662,878109
659,8377914
656,7631531
653,674366
650,5793419
647,4736252
644,3343697
641,1816826
637,9892082
634,7351837
631,4630775
628,1524582
624,7949104
621,441597
618,0762787
614,6556282
611,2047348
607,7260933
604,1986351
600,6916695
597,1775856
593,6737251
590,1740627
586,6520462
583,1032066
579,5335484
575,9418888
572,2927036
568,6520309
565,0026798
561,3029289
557,5445023
553,8214302
550,0660057
546,331953
542,5901661
538,8535595
535,078495
531,2670403
527,4461002
523,6094532
519,7314701
515,8522968
511,9548149
508,0207596
504,0772438
500,099247
496,1326199
492,1450768
488,1371136
484,1382904
480,148838
476,1214924
472,0706844
468,0183048
463,9987135
460,0131788
456,0639172
452,0898123
448,1142168
444,0952234
440,0783987
436,0817747
432,0573263
428,0301075
424,0182018
419,9778624
415,9436293
411,8878984
407,8268013
403,7522116
399,667367
395,5705299
391,4477396
387,2903585
383,0973792
378,8956022
374,7063293
370,4989495
366,3026361
362,1031857
357,8988914
353,6421391
349,4083403
345,1837729
340,954885
336,7690484
332,5892714
328,3917979
324,184617
319,9066211
315,6401675
311,3807096
307,1364915
302,8867259
298,662322
294,3881311
290,1045914
285,8153453
281,5191188
277,2047057
272,8877907
268,5824566
264,3028679
259,9526491
255,5955029
251,2471371
246,8789482
242,5040712
238,1759825
233,8321419
229,5245934
225,1784935
220,8183832
216,4481764
212,0515871
207,6205864
203,1971607
198,7487516
194,282711
189,8287754
185,3651676
180,9052849
176,4728432
172,0135574
167,5855312
163,1710072
158,7539101
154,3510094
149,9402599
145,5038319
141,0639534
136,5862408
132,0802898
127,5227547
122,9710331
118,4548054
113,9910431
109,5285301
105,1010208
100,649931
96,14990425
91,65740967
87,16803741
82,63998032
78,13925934
73,6403904
69,17707062
64,7454567
60,33579826
55,9569416
51,62160683
47,26870441
42,95739365
38,64916515
34,34954357
30,0686388
25,814394
21,60755253
17,43239021
13,31250477
9,219372749
5,108977318
1,046697617
-2,965080261
-6,904607773
-10,83297539
-14,6758256
-18,4493413
-22,20400238
-25,91338253
-29,60839367
-33,25358057
-36,88034296
-40,48026848
-44,04932499
-47,59491897
-51,12528276
-54,63012147
-58,13063574
-61,57121122
-64,98865426
-68,35112846
-71,658775
Coralie B 15-Jun-15 8:51am

Somme pour 20
15870,43508
15869,654
15861,59138
15830,87466
11974,60856
10504,06228
8920,774673
7282,355052
5584,772336
3840,089931
2057,106741
318,3820505
-1186,002578
-2371,585955
-3052,460617
-3183,458002
-3177,692136
-3175,324549
-3175,329502
-3175,146036
-3175,395781
-3176,066615
-3176,26439
-3175,86166
-3176,156509
-3176,295862
Coralie B 15-Jun-15 8:51am

Moyenne pour 20
793,5217539
793,4827
793,0795692
791,543733
764,2582264
720,5081102
664,0709457
598,730428
525,2031138
446,0387336
364,1177526
279,2386168
192,0044966
102,855337
15,91910253
-59,3001289
-118,5792977
-152,6230309
-159,1729001
-158,8846068
-158,7662274
-158,7664751
-158,7573018
-158,769789
-158,8033307
-158,8132195
-158,8147931
Coralie B 15-Jun-15 8:52am

Moy_bar_z
6251,103873
5449,70667
4648,483038
4007,113094
3210,201759
2429,355278
1690,129554
1008,640477
394,3781388
-143,5151832
-598,2860345
-967,4351327
-1247,5253
-1435,674829
-1530,483277
-1535,570795
-1466,071306
-1341,774274
-1193,114349
-1044,655812
0
0
0
0
0
0
0
0
Coralie B 15-Jun-15 9:02am

Barycentre_z :
It's what I have before I do the sum neither average : my positions, around 600 lines.

Somme pour 20 :
It's what I obtain when I summe data by group of 20 data (which come from Barycentre_z), around 30 lines. (600/20)

Moyenne pour 20 :
It's what I obtain when I divide all values of somme pour 20, by 20 : it's what I have to obtain with the average.

Moy_bar_z :
It's what I obtain at the end of my average...

OriginalGriff 15-Jun-15 10:18am

What I meant was "Show me what code you have", not "show me what data you have" :laugh:
OriginalGriff 15-Jun-15 11:23am

1) Don't post your code as a solution, unless it is actually the fixed problem.
2) You didn't listen to a word I said, did you?
Coralie B 16-Jun-15 2:03am

My code is a solution, it works.

And about your reply, you said to me to show you the code I have, and the code I had at this moment, it's in ly question.
Rate this:

## Solution 3

int a = -1;
int f = 1;

Moy_bar_x = new double[Barycentre_x.Length / 20];
Moy_bar_y = new double[Barycentre_y.Length / 20];
Moy_bar_z = new double[Barycentre_z.Length / 20];

while (a < (Barycentre_z.Length / 20)-1) {

a++;
while ((f % 20) !=0) {

Moy_bar_x [a] += Barycentre_x[f];
Moy_bar_y [a] += Barycentre_y[f];
Moy_bar_z [a] += Barycentre_z[f];

f++;

}

f += 1;

}

for (int b = 0; b < Moy_bar_z.Length; b++) {

Moy_bar_x[b] /= 20;
Moy_bar_y[b] /= 20;
Moy_bar_z[b] /= 20;

}