Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: double[][] and double[,] [duplicate]

I confuse between double[][] and double[,] in C#.
My teammate give me a function like this:

public double[][] Do_Something(double[][] A)
{
     .......
}

I want to use this function:

double[,] data = survey.GetSurveyData(); //Get data
double[,] inrma = Do_Something(data);

It lead an error: invalid argument.
I don't want to edit my teammate's code.
Does it have any way to convert double[][] to double [,] ?

Thanks!

like image 920
Ngo Van Avatar asked Jul 24 '13 12:07

Ngo Van


2 Answers

A double[][] is an array of double[] (An array of arrays) but double[,] is a single 2 dimensional double array

Example :

double[] Array1 = new double[] {1,2,3};
double[] Array2 = new double[] {4,5,6};
double[][] ArrayOfArrays = new double[][] {Array1,Array2};
double[,] MultidimensionalArray = new  double[,] {{1,2}, {3,4}, {5,6}, {7,8}};   
like image 52
Siraj Mansour Avatar answered Oct 02 '22 20:10

Siraj Mansour


double[][] and double[,] have different meanings.

double[][] is jagged, so some elements can be of different lengths than others.

double[,] is "rectangular", so all elements are of the same length.

You could write a method to "convert" between the two, but how will you rectify the differences? I.e. how will you decide to "trim" from the long elements or expand the short elements in order to make it rectangular?

like image 29
Mark Avenius Avatar answered Oct 02 '22 20:10

Mark Avenius