【发布时间】:2016-04-04 23:28:06
【问题描述】:
感谢您浏览此页面。我是 C# 新手,我想知道为什么这段代码会增加值。本质上,我希望测量系统中单个粒子的速度,但是,文本文件的结果显示速度的每个值都会添加到下一个。我需要将每个速度值的计数放入一个 bin 中,例如将 0 到 0.5 之间的速度计数 1 放入 bin 1 并将 0.5 到 1.0 放入 bin 2 等等,然后每个值bin 可以写入一个文本文件,然后我可以将其转换为直方图,以查看某个 bin 中每个速度的出现频率。我的一段代码如下。我非常感谢我得到的任何帮助。感谢您查看该页面。
private void computeVelocity()
{
//calc velocity of each particle for printing
double binSize = 0.5;
double velocityOfParticle = 0.0;
double averageSquareVelocity = 0.0;
int maxArrayValue = 0;
int binArraySize = 0;
int z;
double[] velocityArray = new double[Particle.allParticles.Count];
for (int counter = 0; counter < Particle.allParticles.Count; counter++)
{
averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
velocityOfParticle = Math.Sqrt(averageSquareVelocity);
velocityArray[counter] = velocityOfParticle;
}
maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
//numberOfBins = Math.Round(maxArrayValue / binSize);
int [] binCountArray = new int[numberOfParticles];
int incrementTerm = 0;
Array.Clear(binCountArray, 0, binCountArray.Length);
for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
{
if(counter > binSize)
{
incrementTerm = incrementTerm + 1;
}
z = 0;
for(int i = 0; i < numberOfParticles; i++)
{
if(velocityArray[i] <= counter)
{
z++;
}
}
binCountArray[incrementTerm] = z;
}
foreach (var item in binCountArray)
{
VelocityValues.WriteLine(item);
}
}
【问题讨论】:
-
因为 Math.Round 语法。
-
有什么问题?您的标题说它会增加,并且您的描述描述了您希望如何增加 bin 来计算每个速度组的频率。这里没有问题定义。
标签: c# arrays increment recording bin