Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with 2D arrays

Tags:

arrays

c#

I'm trying to fill some data into two arrays, one containing normalized angles and another containing the sin of those angles. The arrays have to be 2D because they're going to be passed into a function that trains a neural network. I'm tried declaring a [1][360] array and got errors, so I've also tried [1][] as that's what intellisense is telling me, but then I got another problem.

Here is my code:

double[][] sin_in = new double[1][];
double[][] sin_out = new double[1][];
double deg = 0.0;
const double dtor = 3.141592654 / 180.0;

for (int i = 0; i < 360; i++)
{
    sin_out[0][i] =  Math.Sin(deg * dtor); // complains I need to use new
    sin_in[0][i] = deg / 360.0; //When I use new I get another error
    deg += 1.0;
}
IMLDataSet trainingSet 
    = new BasicMLDataSet(sin_in, sin_out); //Inputs must be [][]

So what mistakes/misunderstandings have I made?

like image 523
James Avatar asked Jul 26 '14 13:07

James


1 Answers

You initialize a two dimensional array like this:

double[,] sin_in = new double[1, 360];
double[,] sin_out = new double[1, 360];
double deg = 0.0;
const double dtor = 3.141592654 / 180.0;

for (int i = 0; i < 360; i++)
{
    sin_out[0,i] =  Math.Sin(deg * dtor); // complains I need to use new
    sin_in[0,i] = deg / 360.0; //When I use new I get another error
    deg += 1.0;
}

Oh, and by the way, the value of PI is built in in c# with as many decimals that fits in a double, use

const double dtor = Math.PI / 180.0;
like image 153
Albin Sunnanbo Avatar answered Sep 26 '22 16:09

Albin Sunnanbo