views:

107

answers:

6

There a section in my code where I need to invert a matrix. That can only reasonably be done on a square matrix, in this case a 3x3 square matrix. The tool I'm using to invert the matrix kept saying that my array wasn't a proper square.

So I did a little test:

double[,] x = new double[3, 3];

MessageBox.Show(x.GetLength(0).ToString());
MessageBox.Show(x.GetLength(1).ToString());
MessageBox.Show(x.GetLength(2).ToString());

First one comes up as "3". Second one comes up as "3". Third one comes up as an IndexOutOfRangeException. Am I just overlooking something extremely obvious or... is this a little weird?

(Note: This is code from C# using .Net 2.0)

+11  A: 

You only have an array with two dimensions. Why would you expect asking for the size of the third dimensions to give you a valid result?

The Array.GetLength() method return the number of elements in the specified dimension of the Array. In your case:

x.GetLength(2).ToString();   // asking for size of third dimension

you're asking a 2-dimensional array what the size of it's third dimension is. The result is an IndexOutOfRangeException. This is the expected behavior.

In your code example, it looks like you may be confusing the size of each stated dimension, with the number of dimensions. Here are some examples of rectangular arrays of different dimensions:

var d1 = new int[5];     // one dimensional array, containing 5 elements
var d2 = new int[3,3];   // two-dimensional 3x3 element array
var d3 = new int[2,2,2]; // three-dimension array of 2x2x2 elements
var d4 = new int[2,5,6,8]; // four dimensional array, of 2x5x6x8 elements

See the pattern? The number of dimensions is determined by how many numbers you specify in the array declaration. The sizes of each dimension is determined by the values of each number in the declaration.

LBushkin
Well... like I said, probably overlooking something obvious. Doesn't help that I'm basing it on a VB project that did it the way I did, and somehow succeeded...Thanks. I have these brain farts every now and again.
KChaloux
@KChaloux: Maybe you are confusing checking the lengths of jagged arrays with the lengths of dimensions in a multidimensional array?
Eric Mickelsen
Id add what a 3 dimensional array looks like: double[,,] x = new double[3,4,5];
SwDevMan81
@tehMick: I'm pretty sure you're right.
KChaloux
+4  A: 

Your matrix only has 2 dimensions, both having length of 3 elements. The third MessageBox line is trying to print the length of a non-existent third dimension.

thegravytalker
A: 

You don't have 3 dimensions defined in your array, so you can only get the length of two. Your code is asking for the dimensions of a cube, not a square.

double[,] x = new double[3,3] // This sets up a two-dimensional array

MessageBox.Show(x.GetLength(0).ToString()); //Shows the length of the X axis
MessageBox.Show(x.GetLength(1).ToString()); //Shows the length of the Y axis
MessageBox.Show(x.GetLength(2).ToString()); //Trys to show the length of Z Axis.
AllenG
+1  A: 
double[,,] x = new double[3, 3, 3];

MessageBox.Show(x.GetLength(0).ToString());
MessageBox.Show(x.GetLength(1).ToString());
MessageBox.Show(x.GetLength(2).ToString());
Chris Almond
+3  A: 

Just to provide a visual example and provide an answer in my own words... (I failed at first to see why the exception was being thrown, also):

double[,] x = new double[3, 3]; gives an array that looks like this:

[0,0] [1,0] [2,0]
[0,1] [1,1] [2,1]
[0,2] [1,2] [2,2]

I thought that the code was trying to find the length of what might be thought of as a subarray, where each row represents an array that is in turn a member of a larger array.

In actuality the rows are not themselves arrays, just members (doubles) of the original array. The GetLength method is looking for the dimensions of the array rather than the length (which might be confusing because Length is used to determine the number of members in an array). This array has two dimensions, the X dimension and the Y dimension, if you will.

Think of it like a flat sheet, which has two dimensions. If you add a third:

double[,,] x = new double[3, 3, 3];

The sheet becomes a cube, a new dimension Z is added, and the third GetLength would work.

JYelton
A: 

This may help you visualize what's been explained.

Your array of x as you've declared it looks like this:

double[,] x = new double[3, 3];

Dimensions     0,1    0,1    0,1            
Element       [0,0], [0,1], [0,2]   
Element       [1,0], [1,1], [1,2]
Element       [2,0], [2,1], [2,2]
Lengths        3,3    3,3    3,3
Chuck