查找列表中的更改,然后将特定值存储到数组中
本文关键字:数组 存储 然后 列表 查找 | 更新日期: 2023-09-27 18:03:07
我有一个类:
namespace XMLParserAverage11
{
public class cPoint
{
public string point;
public string time;
public double xPoint;
public double yPoint;
public double StdDevX;
public double StdDevY;
public string csv;
public cPoint(string n, double x, double y)
{
this.point = n;
xPoint = x;
yPoint = y;
}
}
}
用于存储从读取XML文件获得的信息,然后将其放入
List<cPoint> Sorted
我想做的是将"xPoint"存储到一个数组中,并将"yPoint"存储到另一个数组中,用于所有具有相同值的"point"。这样我就可以最终使用Mathnet中的Statistics.PopulationStandardDeviation()
。数字NuGet包,它需要一个双数组,并计算xPoint和yPoint的总标准偏差每个相同的"点"在列表排序。
if (measurementType == "Body")
{
double a = Convert.ToDouble(xOffset);
double b = Convert.ToDouble(yOffset);
cPoint Point = new cPoint(location, a, b);
Point.time = endTime;
// Point.point = location;
// Point.xPoint = Convert.ToDouble(xOffset);
// Point.yPoint = Convert.ToDouble(yOffset);
sorted.Sort((x, y) => x.point.CompareTo(y.point));
// double[] balanceX = {Point.xPoint};
// double[] balanceY = {Point.yPoint};
if (sixSig == true)
{
List<cPoint> pointList = new List<cPoint>();
// Select all the distinct names
List<string> pointNames = location.Select(x => xOffset).Distinct().ToList();
foreach (var name in pointNames)
{
// Get all Values Where the name is equal to the name in List; Select all xPoint Values
double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();
Point.StdDevX = Statistics.StandardDeviation(x_array);
// Get all Values Where the name is equal to the name in List; Select all yPoint Values
double[] y_array = pointList.Where(n => n.point == name).Select(x => x.yPoint).ToArray();
Point.StdDevY = Statistics.StandardDeviation(y_array);
}
csvString = Point.time + "," + Point.point + "," + Point.xPoint + "," + Point.yPoint + "," + Point.StdDevX + "," + Point.StdDevY;
Point.csv = csvString;
sorted.Add(Point);
}
else
{
csvString = endTime + "," + location + "," + xOffset + "," + yOffset;
Point.csv = csvString;
sorted.Add(Point);
}
}
我的输出最终会像这样:
显然,我是初学者,所以任何帮助都会非常感激。
EDIT:
ok这是一个小的控制台应用程序,我认为它说明了你正在寻找的过程。为此,我削减(并重命名)了您的类cPoint
:
public class C_Point
{
public string point;
public double xPoint;
public double yPoint;
public C_Point(string n, double x, double y)
{
this.point = n;
xPoint = x;
yPoint = y;
}
}
class Program
{
static void Main(string[] args)
{
List<C_Point> pointList = new List<C_Point>();
pointList.Add(new C_Point("P1", 1, 11));
pointList.Add(new C_Point("P1", 2, 22));
pointList.Add(new C_Point("P1", 3, 33));
pointList.Add(new C_Point("P10", 101, 111));
pointList.Add(new C_Point("P10", 201, 211));
pointList.Add(new C_Point("P10", 301, 311));
// Select all the distinct names
List<string> pointNames = pointList.Select(x => x.point).Distinct().ToList();
foreach (var name in pointNames)
{
Console.WriteLine("Name: {0}", name);
// Get all Values Where the name is equal to the name in List; Select all xPoint Values
double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();
Console.WriteLine(String.Join(" ", x_array));
// Get all Values Where the name is equal to the name in List; Select all yPoint Values
double[] y_array = pointList.Where(n => n.point == name).Select(x => x.yPoint).ToArray();
Console.WriteLine(String.Join(" ", y_array));
}
Console.ReadKey();
}
}
你可以把它复制粘贴到一个控制台项目中,然后让它运行。它将显示带有点名称的数组值,在您的例子中是Location
。我接近你的问题了吗?
当你试图获得所有不同的点的名称时,你需要从所有点所在的List
中提取它们。
这里你试着从string
中选择一些东西。这行不通。
// Select all the distinct names
List<string> pointNames = location.Select(x => xOffset).Distinct().ToList();
请使用sorted
List。我想你把它们都放进去了,对吧?Select(x=>x.point)
意味着:x
是列表中任何元素(在类型cPoint
的情况下)的占位符。在=>
之后指定应该选择元素的哪个属性!在本例中是x.point
。结果将是所有元素的该属性的所有值的列表。Distinct
调用消除了多个条目。
This: (x => xOffset)
不工作,因为它不是一个有效的表达式,因为你没有可以在你的类cPoint
中选择的属性Offset
这个也不行:
double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();
因为你的pointList
是空的!你需要再次使用List
与你所有的点!
希望这对你有帮助。如果不行就给我写信。
有关更多信息,请阅读有关Lambda表达式的更多信息