This project has moved. For the latest updates, please go here.

Basic question - index of matrices (zeroth element)

Oct 24, 2012 at 9:09 PM

The matrices used in this library have a 1 based index or a x zero based index?


Does DenseMatrix(0,0) return anything?

Oct 24, 2012 at 9:54 PM
  • Everything is zero-based, including column and row indices, as common in .Net (convention).
  • Matrices must have at least one row and column, hence "new DenseMatrix(0,0)" will fail with an ArgumentOurOfRange exception.
Oct 25, 2012 at 12:22 AM
Edited Oct 25, 2012 at 12:23 AM

Thanks for the quick reply.  Just to be clear though...

While in .Net convention

Dim A(10,1) as Integer     'will create an array of 22 items

Dim Amat As New DenseMatrix(10,1)        'will create 10x1 (20 element) matrix.




Oct 25, 2012 at 9:01 AM
Edited Oct 25, 2012 at 9:03 AM

No, the second line will create a 10x1 (hence 10 elements - 10 rows, 1 column) matrix.

By convention, in .Net you access the first element by index 0, but specify size by number of elements (here number of rows and columns)

Note that the first line is a special case for arrays, where VB.Net follows old VB6 conventions instead of .Net conventions - Array initialization is special VB syntax, which actually translates to .Net/IL "newobj int[,]::.ctor(11,2)", which then indeed does follow .Net conventions. On the other hand, the second line calls a normal constructor which is pure .Net, where no VB6 conventions (can) apply - same with all other BCL collection types like List.

Oct 26, 2012 at 1:23 AM


Remind me to check my arithmetic before posting.   10 elements was indeed what I meant to post!

I was just reminding/informing that in at least one .Net language (vb is a .net compliant language) that array initialization defines the size+1 of the array.

It does make for some confusion in using matrices vs arrays in the same code as size translation must occur (as I found recently in my code).



Oct 30, 2012 at 6:44 PM
Edited Nov 18, 2012 at 10:06 AM

//It does make for some confusion in using matrices vs arrays in the same code as size translation must occur (as I found recently in my code).

Are you are porting your code from another language, e.g. Matlab or R or another 1 based librarry ?If you go to I wrote one-based methods for the library that are added to the existing methods. I added a Roman I to the name of the 0 based indexing method because Romans did not have a number 0. 

mymatrix.SetRowI(1,..)  is equivalent to mymatrix.SetRow(0,..)

myMatrix.AtI(1,5) is equivalent to mymatrix.At(0,4) etc.

Same for vectors. Multiplication etc. all stay the same.

I am porting a lot of C++ code that made use of a 1 based matrix library and the indexing was also used in many classes outside the matrixes. I think 0 based indexing is confusing when implementing math algoritms, but it is the norm back from C days in arrays and is the norm in most but not all C++ libraries.  Why would anybody count the six eggs in a tray as egg 0, egg 1, egg2...egg5 is beyond me. It does work quite well, where the index indicates time delays . When dealing with datasets talking about row 0 , column 0 is confusing for a normal users, so then one has to translate everything in the user interface back to 0 again.

I recommend that if you are starting to code from scratch or have a small code base rather use the 0 based indexing, that is also the norm in most c# application. Translate back to 1 based indexing in the gui if required.