我有一个班级:
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”存储到另一个数组中,用于所有“点”that has the same value。这样我最终可以使用Mathnet.Numerics NuGet包中的Statistics.PopulationStandardDeviation()
,它需要一个双数组,并计算列表排序中每个相同“点”的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);
}
}
我的输出最终会与this类似 显然我是初学者,所以任何帮助都会非常感激。
答案 0 :(得分:0)
编辑:
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
。
我接近你的问题了吗?
编辑2:
当您尝试获取积分的所有不同名称时,您需要从所有积分所在的List
中提取它们。
在这里,您尝试从string
中选择一些内容。这不起作用。
// Select all the distinct names
List<string> pointNames = location.Select(x => xOffset).Distinct().ToList();
而是使用sorted
列表。我想你们都在那里,不是吗?
Select(x=>x.point)
表示:x
是列表中任何元素(在您的类型为cPoint
的情况下)的占位符。在=>
之后指定应该选择元素的哪个属性!在这种情况下x.point
。结果将是所有元素的此属性的所有值的列表。 Distinct
调用会删除多个条目。
这:(x => xOffset)
不起作用,因为它不是有效的表达式,因为您没有可以在班级Offset
中选择的属性cPoint
这也不起作用:
double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();
因为您的pointList
是空的!您需要再次使用List
以及所有积分!
希望这会对你有所帮助。如果没有,请写信给我。
有关详细信息,请阅读有关Lambda expressions
的更多信息