C#平均每组20个数据 [英] C# To average of each group of 20 data

查看:98
本文介绍了C#平均每组20个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我想平均每组20个Barycentre_x Barycentre_y和Barycentre_z的数据。这些变量有许多cpt线(大约600行)。





我尝试这样做,但它没有' 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 ;

}

// 这里我开始平均值

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 ++;

如果(index_moy% 20 == 0 ){

indice_2 ++;

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





代码的开头:



int index_moy = 0;

int indice_2 = -1;

解决方案

试试这个;

 静态  void  Main(字符串 [] 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];

随机rand = 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 [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]);
}
}


如果你想做一个移动平均线,那么有两种方法可以做到:

1)将最后20个项目保留在数组或类似项目中,并每次更换最旧的项目,然后重新排列数组。

2)保持运行总计,当你有二十个项目时,从总数中减去当前位置二十的值,然后加上当前值。



如果你想把它们加起来二十组,然后只使用两个循环:

外循环迭代组(即二十几岁的移动)

内循环在一个组内迭代。



您只需要两个索引(输出组,输入组)和两个计数。


  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 ;

}


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;

解决方案

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]);
       }
     }


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.


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;

}


这篇关于C#平均每组20个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆