The problem is in the list <double [,]>

What is wrong with this (in C # 3.0):

List<double> x = new List<double> { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090, -0.2718, -0.2240, -0.1275, -0.0810, 0.0349, -0.5067, 0.0094, -0.4404, -0.1212 }; List<double> y = new List<double> { 0.4807, -3.7070, -4.5582, -11.2126, -0.7733, 3.7269, 2.7672, 8.3333, 4.7023,0,0,0,0,0,0,0,0,0 }; List<double[,]> z = new List<double[,]>{x,y}; // this line 

An error has occurred:

 Error: Argument '1': cannot convert from 'System.Collections.Generic.List<double>' to 'double[*,*]' 

Help needed.

+4
source share
5 answers
 var z = new List<List<double>> { x, y }; 

However, if you want to save your two lists in a two-dimensional array ([,]) this is your anders . You will have to convert it manually, as shown in the figure:

 public static T[,] To2dArray(this List<List<T>> list) { if (list.Count == 0 || list[0].Count == 0) throw new ArgumentException("The list must have non-zero dimensions."); var result = new T[list.Count, list[0].Count]; for(int i = 0; i < list.Count; i++) { for(int j = 0; j < list.Count; j++) { if (list[i].Count != list[0].Count) throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes."); result[i, j] = list[i][j]; } } return result; } 
+1
source

double[,] defines a multidimensional array, but you specify two lists.

From your Initialization, it looks like you are looking for something like

 List<PointF> list = new List<PointF> { new PointF (0.0330F, 0.4807F), new PointF (-0.6463F, -3.7070F) }; 
0
source

The collection initializer for List<double[,]> expects elements of type double[,] (which is a two-dimensional array similar to a matrix), but you pass it x and y , which are of type List<double> , which means that it is trying add two doubling lists as elements of the new list.

If you are trying to add coordinates to the list, you need some kind of structure to contain them. You can write your own, or you can use System.Drawing.PointF .

0
source

Are you after something like this?

  List<double> x = new List<double> { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090, -0.2718, -0.2240, -0.1275, -0.0810, 0.0349, -0.5067, 0.0094, -0.4404, -0.1212 }; List<double> y = new List<double> { 0.4807, -3.7070, -4.5582, -11.2126, -0.7733, 3.7269, 2.7672, 8.3333, 4.7023, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; List<double[]> z = x.Select((x1, index) => new double[2] {x1, y[index]} ).ToList(); 

EDIT: Modified my answer to correctly insert lists into the index, rather than looking for it.

0
source

Must not be:

 List<List<double>,List<double>> z = new List<List<double>, List<double>>{x,y}; 

But I don’t think this is really what you need for?

0
source

Source: https://habr.com/ru/post/1308121/


All Articles