I have 6 lists.
List<double> listdSignal0X = new List<double>();
List<double> listdSignal0Y = new List<double>();
List<double> listdSignal1X = new List<double>();
List<double> listdSignal1Y = new List<double>();
List<double> listdSignal2X = new List<double>();
List<double> listdSignal2Y = new List<double>();
I want to group them in XY pairs in a List<List>.
List<List<double>> listlistdXYSignals = new List<List<double>>();
listlistdXYSignals.Add(listdSignal0X);
listlistdXYSignals.Add(listdSignal0Y);
listlistdXYSignals.Add(listdSignal1X);
listlistdXYSignals.Add(listdSignal1Y);
etc.
This does not group them in pairs. I want
listlistdXYSignals[0] ... to retrieve both ... listdSignal0X ... and ... listdSignal0Y
listlistdXYSignals[1] ... to retrieve both ... listdSignal1X ... and ... listdSignal1Y
etc.
The pairs of XY lists are the same length. Each pair of XY lists could be different lengths. Signal1X and Signal1Y are the same length (1000 values). Signal1X is a different length than Signal2X (200 values).
These pairs of XY lists are passed to a function that creates multiple plots on a WPF xaml.
I have tried:
List<Tuple<List<double>, List<double>>> listtupledSignals = new List<Tuple<List<double>, List<double>>>();
listtupledSignals.Add(listdSignalX, listdSignalY);
Error message, "No overload for method 'Add' takes 2 arguments".
How do I group pairs of
List<double>
in a
List<List<double>>
?
List<List<double>, List<double>>
does not work either.
CodePudding user response:
You could create a class to hold the pair of lists:
class SignalListPair
{
public List<double> X { get; set; } = new List<double>();
public List<double> Y { get; set; } = new List<double>();
}
static void Main(string[] args)
{
var signal0XY = new SignalListPair();
var signal1XY = new SignalListPair();
var signal2XY = new SignalListPair();
var signals = new List<SignalListPair>();
signals.Add(signal0XY);
signals.Add(signal1XY);
signals.Add(signal2XY);
var x0 = signals[0].X;
var y0 = signals[0].Y;
var x1 = signals[1].X;
var y1 = signals[1].Y;
}
CodePudding user response:
You need to ensure that the size of the lists are equal.
public class Point
{
public double X { get; set; }
public double Y { get; set; }
}
var listdSignal0Y = new List<double>(new []
{
1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9
});
var listdSignal0X = new List<double>( new []
{
2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9
});
var listdSignal2X = new List<double>(new []
{
3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9
});
var listdSignal2Y = new List<double>(new []
{
4.1,4.2,4.3,4.4,4.5,4.6,4.7,4.8,4.9
});
var listdSignal1X = new List<double>(new []
{
5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9
});
var listdSignal1Y = new List<double>(new []
{
6.1,6.2,6.3,6.4,6.5,6.6,6.7,6.8,6.9
});
var listdXYSignals = new List<Point>();
if (listdSignal0X.Count == listdSignal0Y.Count)
{
for (var i = 0; i < listdSignal0X.Count; i )
{
listdXYSignals.Add(
new Point
{
X = listdSignal0X[i],
Y = listdSignal0Y[i]
});
}
}
foreach (var o in listdXYSignals)
{
Console.WriteLine($"X: {o.X} - Y: {o.Y}");
}
CodePudding user response:
Try using tuples; they are a very clean solution to your problem
var listOfXYPairs = new List<(double, double)>();
listOfXYPairs.Add((listdSignal0X, listdSignal0Y)); // remember about the ((x, y)), a tuple (x, y) is a single argument
CodePudding user response:
It works to create:
List<Point> listPointsTop = new List<Point>();
List<Point> listPointsMiddle = new List<Point>();
List<Point> listPointsBottom = new List<Point>();
then
CreateXYPoints(listdSignal0X, listdSignal0Y, ref listPointsTop);
CreateXYPoints(listdSignal1X, listdSignal1Y, ref listPointsMiddle);
CreateXYPoints(listdSignal2X, listdSignal2Y, ref listPointsBottom);
private void CreateXYPoints(List<double> listdSignalX, List<double> listdSignalY, ref List<Point> listPoints)
{
int iNumOfPoints = listdSignalX.Count;
for (int ii = 0; ii < iNumOfPoints; ii ) {
double dX = listdSignalX[ii];
double dY = listdSignalY[ii];
Point pt = new Point(dX, dY);
listPoints.Add(pt);
};
}//CreateXYPoints
Then use:
List<List<Point>> listlistpoxySignals = new List<List<Point>>();
listlistpoxySignals.Add(listPointsTop);
listlistpoxySignals.Add(listPointsMiddle);
listlistpoxySignals.Add(listPointsBottom);
listlistpoxySignals is passed to the plot xaml function.
CodePudding user response:
You could try zipping them up using LINQ if they are of equal length.
List<List<(double,double)>> listlistdXYSignals = new List<List<(double,double)>>();
listlistdXYSignals.Add(listdSignal0X.Zip(listdSignal0Y).ToList());
listlistdXYSignals.Add(listdSignal1X.Zip(listdSignal1Y).ToList());
etc.
CodePudding user response:
I think what you need is a List<Tuple<List<double>, List<double>>>
Ref.: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples