James
James

Reputation: 4052

issue with 2D arrays

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?

Upvotes: 5

Views: 232

Answers (3)

James
James

Reputation: 4052

OK, it seems like the function rejects multidimensional arrays but accepts jagged arrays. To form my jagged array correctly I changed my code to this:

        double[][] sin_in = new double[360][];
        double[][] sin_out = new double[360][];
        double deg = 0.0;
        const double dtor = Math.PI / 180.0;

        for (int i = 0; i < 360; i++)
        {
            sin_out[i] =  new [] {Math.Sin(deg * dtor)};
            sin_in[i] =  new[] {deg / 360.0};
            deg += 1.0;
        }

Upvotes: 0

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47068

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;

Upvotes: 6

cynic
cynic

Reputation: 5415

You're using jagged arrays (aka. arrays of arrays). If you need multi-dimensional arrays, use the double[,] array = new double[10,10]; syntax.

Upvotes: 4

Related Questions