0

I have been programming with Matlab for a couple of years. It handles operations on matrices and arrays quite easily.

Now, I am programming in C# and after reading for a while on sites such as http://msdn.microsoft.com/en-us/library/2s05feca.aspx and various other tutorial websites, I still have some questions.

In Matlab, something like this (written in a pseudo C# syntax) would work:

W[1][][] = new double[][]{{10,20,30},{40,50,60},};

where W is a 3 dimensional jagged array. The idea is that since the first dimension is defined (by 1 in this case), the assignation operation "understand" that the content of the 2 dimensional array provided is set to fill the two remainder dimensions of W. Then, how to get the same result or behaviour in C#?

Imagine the first dimension of W[][][] is a number associated with a city; the second, the age of every people living there and the third, their weight. Now lets have a portion of code that create a "calculation[][]" array containing ages and weights values. I want to assign the content of this array to W[1][][]. That is what I have failed to implement.

My objective is to do the equivalent of multiplication of matrices (and other relevant operations on matrices) using C#.

I read and read again a couple of explanations (can't list them all here) but it does not add up in my head, so I guess if someone could explain (again) how to manipulate multidimensional arrays in C#, it will help me understand how it works.

Thanks

2
  • On the left, W is a double[][][], while on the right you are creating a double[][] That doesn't match. If each dimension has the same length, a multidimensional array may be more convenient: double[,,] or double[,] Commented Jul 4, 2014 at 19:09
  • Sorry, my question is not clear. I know this syntax won't work directly in C#. Is there a way to assign two of the three dimensions of a jagged array at the same time. Imagine the first dimension of W[][][] is a number associated with a city; the second, the age of every people living there and the third, their weight. Now lets have a portion of code that create a "calculation[][]" array containing ages and weights values. I want to assign the content of this array to W[1][][]. That is what I have failed to implement Commented Jul 4, 2014 at 19:19

3 Answers 3

1

This isn't a 3 dimensional array. This is just an array of an array. I'm not really sure what you try to do.

Anyway, If I got u right u want to do something like this:

Nothing -.-

Ok, so if you need this for your example then I would recommend you a struct in order to do this easier. If you want to understand Multidimensional Arrays then you should reed the following:

You defined your 'Multidimensional Array' like this:

double[][][] array;

That's not a 'Multidimensional Array'. This is an array (list) of an array of an array.

A real 'Multidimensional Array' is defined this (3 dimensions):

double[,,] array;

That's a difference :D

if u use the 'real Multidimensional Array' then u can do the above like this;

private static void Main(string[] args)
{
    var raw = new double[,,]
    {
        {
            {1, 2},
            {3, 4}
        },
        {
            {5, 6},
            {7, 8}
        }
    };

    Console.WriteLine(raw[0, 0, 0]); //1
    Console.WriteLine(raw[1, 1, 0]); //7
    Console.ReadKey();
}

And this would be the code for the array of an array of an array

var raw = new double[][][]
{
    new double[][]
    {
        new double[]
        {
            1,
            3
        },
        new double[]
        {
            2,
            4
        }
    },
    new double[][]
    {
        new double[]
        {
            5,
            7
        },
        new double[]
        {
            6,
            8
        }
    }
};
Console.WriteLine(raw[0][0][0]); //1
Console.WriteLine(raw[1][1][0]); //6
Console.ReadKey();

In your case (if you don't want to use a struct) than this array would be the better way!

A struct for your case would look like this:

    private static void Main(string[] args)
    {
        var raw = new SampleDataStruct[2]
        {
            new SampleDataStruct
            {
                CityName = "New York", AgeWeight = new AgeWeightStruct[3]
                {
                    new AgeWeightStruct{Age = 50,Weigth = 70},
                    new AgeWeightStruct{Age = 40,Weigth = 75},
                    new AgeWeightStruct{Age = 30,Weigth = 65}
                }
            },

            new SampleDataStruct
            {
                CityName = "Berlin", AgeWeight = new AgeWeightStruct[3]
                {
                    new AgeWeightStruct{Age = 50,Weigth = 65},
                    new AgeWeightStruct{Age = 40,Weigth = 60},
                    new AgeWeightStruct{Age = 30,Weigth = 55}
                }
            }
        };

        Console.WriteLine(raw.Where(st => st.CityName == "Berlin").ElementAtOrDefault(0).AgeWeight.Where(aw => aw.Age == 40).ElementAtOrDefault(0).Weigth); //Berlin -> Age = 40 -> Display Weight (60)
        Console.WriteLine(raw.Where(st => st.CityName == "New York").ElementAtOrDefault(0).AgeWeight.Where(aw => aw.Age == 30).ElementAtOrDefault(0).Weigth); //New York -> Age = 30 -> Display Weight (65)
        Console.ReadKey();
    }
}

public struct SampleDataStruct
{
    public string CityName;
    public AgeWeightStruct[] AgeWeight;
}

public struct AgeWeightStruct
{
    public int Age;
    public int Weigth;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah a struct is what I was looking for. Dumb me I use them in Matlab forgot they were in Csharp too...
@Do I added a struct for your case!
1

You can do it so:

            Int32 firstDimensionSize = 3;
            double[][][] matrix3D = new double[firstDimensionSize ][][];

            matrix3D[1]= new double[][]
            { 
                new double[]{10,20,30}, 
                new double[]{40,50,60},
            };

Because matrix3D is an array of arrays of arrays. Each its element is double[][] - 2D jagged array. Just don't forget that matrix3D[0] and matrix3D[2] will be uninitialized - null.

2 Comments

Makes me think, just another question: Is there some way to incorporate a for loop to initialize all values to 0? Instead of having new double[]{0,0,0,0,0,0 ... 0} one thousand time?
Ok it seems I can do this: double[] banana = new double[]{}; for(int i=0,i<1000,i++) { banana[i]=0; } then: matrix3D[1]= new double[][] { banana, banana, };
1
var W = new double[][][]
{
    new double[][]{
        new double[] {1,2,3}, new double[] {4,5,6}
    },
    new double[][]{
        new double[] {7,8,9}, new double[] {10,11,12}
    }
};

Alternatively, you can use a double[,,] if the dimensions are the same:

var W = new double[,,]
{
    {
        {1,2,3}, {4,5,6}
    },
    {
        {7,8,9}, {10,11,12}
    }
};

1 Comment

Sorry I am not yet allowed to upvote but it works too! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.