Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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

Try this;
C#
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]);
       }
     }
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
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?
C#
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;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900