pythonarraysnumpymatrixdimensional

Multidimensional Matrix in Numpy


I read something about NumPy and it's Matrix class. In the documentation the authors write, that we can create only a 2 dimensional Matrix. So I think they mean you can only write something like this:

input = numpy.matrix( ((1,2), (3,4))

Is this right? But when I write code like this:

input = numpy.matrix( ((1,2), (3,4), (4,5)) )

it also works ... Normally I would say ok, why not, I'm not intrrested why it works. But I must write an exam for my univerity and so I must know if I've understood it right or do they mean something else with 2D Matrix?

Thanks for your help


Solution

  • They both are 2D matrixes. The first one is 2x2 2D matrix and the second one is 3x2 2D matrix. It is very similar to 2D arrays in programming. The second matrix is defined as int matrix[3][2] in C for example.

    Then, a 3D matrix means that it has the following definition: int 3d_array[3][2][3].

    In numpy, if i try this with a 3d matrix:

    >>> input = numpy.matrix((((2, 3), (4, 5)), ((6, 7), (8, 9))))
    ValueError: matrix must be 2-dimensional