2

I think the title is quite clear, so I'll just write some personal opinions here.

Consider a matrix of numbers, the equivalent representations in C# code are double[,] and double[][] respectively. When using multi-dimensional array (2D in this specific situation), It can be easily seen that one doesn't have to check either there is any null reference of double[] or the size of rows are the same, which allows a better understanding of the core problem. Also it descirbes the matrix more accurately from my point of view, since in most cases a matrix should be treated as a single entity rather than a list of arrays.

But using multi-dimensional array may result in more lines of code. If one wants to apply math operations on it, say, transposition, he would have to use nested loops like

var row = mat.GetLength(0);
var col = mat.GetLength(1);
var newmat = new double[col, row];
for (var i = 0; i < row; i++)
{
    for (var j = 0; j < col; j++)
    {
         newmat[j, i] = mat[i, j];
    }
}

With jagged array, he can simply write

var newmat = Enumerable.Range(0, mat[0].Length - 1).
    Select(i => mat.Select(r => r[i]).ToArray()).ToArray();

I'm not sure which one is better. Usually I only create my own subroutine unless there is no solution provided by .Net, so I prefer the latter. But multi-dimensional array do have its advantages which I really like. Could anyone teach me about how to choose between them?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Tommy T
  • 55
  • 7
  • Check this answer [What are the differences between a multidimensional array and an array of arrays in C#](http://stackoverflow.com/a/597729/3254920) – Abdul Hadi Jun 11 '16 at 05:29
  • 1
    Jagged arrays are a bit clumsy in code, but much more efficient at runtime. The array bounds checking for multi-dimensional arrays is hard to eliminate. When you also eliminated the clumsiness then it is a no-brainer. – Hans Passant Jun 11 '16 at 09:11
  • Yeah, I read that answer, thanks Abudl. But in this case, I think I would give more concern about the readablity of the code rather than performance issue. – Tommy T Jun 12 '16 at 03:43
  • Well when I say readablity I actually mean that more 'mathematically' similar to the problem. – Tommy T Jun 12 '16 at 05:08

1 Answers1

1

It's not about the lines of code that is the problem, but the efficiency of the code itself.

If you had a sparse matrix (matrix with almost all zeros), you want to use a jagged matrix because iterating through the two-dimensional matrix searching for non-zero elements would waste time.

However, if you had a matrix and you wanted to find its determinant, it would be simpler to use the method of co-factors on it. If you're not familiar with the method, it involves breaking up the matrix into smaller matrices, eventually to the 2x2 version where you can simply perform a*d-b*c. This isn't possible with jagged matrices.

Jossie Calderon
  • 1,393
  • 12
  • 21
  • So basically, use the one suits the problem best, am I right? – Tommy T Jun 12 '16 at 04:50
  • Yeah, but now we come to a new problem. If you use different structures in different modules, how to convert between them? Do I have to iterate every element using nested for loops, or is there a better way? – Tommy T Jun 12 '16 at 05:05
  • No, you can't magically fill a two-dimensional array all at once with distinct elements, i.e. `with complexity O(1)`. You can perform `array.fill(x);` which fills your array with the element `x`. But you will have to go through every cell if you want to input two distinct values or more. Either way, all operations involve going through every cell of the array. Does this answer your question? – Jossie Calderon Jun 12 '16 at 05:10
  • Yes, though I'm a little disappointed that there is no better way. Thank you for your kind reply. :) – Tommy T Jun 12 '16 at 05:22
  • @TommyT: Make sure to click the up-arrow if it helped you and click the check-mark if it resolved your issue. You are welcome. – Jossie Calderon Jun 12 '16 at 05:49