Collin
Collin

Reputation: 1867

Numpy matrix must be 2-dimensional error

I have the following code:

import numpy as np
def J(x, y):
    return np.matrix([[8-(4 * y), -4 * y], [y, -5 + x]])

x_0 = np.matrix([[1], [1]])
test = J(x_0[0], x_0[1])

When I go to run it I receive the following error:

Traceback (most recent call last):
  File "broyden.py", line 15, in <module>
    test = J(x_0[0][0], x_0[1][0])
  File "broyden.py", line 12, in J
    return np.matrix([[8-(4 * y), -4 * y], [y, -5 + x]])
  File "/home/collin/anaconda/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 261, in __new__
    raise ValueError("matrix must be 2-dimensional")
ValueError: matrix must be 2-dimensional

I don't understand why I'm getting this error. Everything appears to be 2-d.

Upvotes: 0

Views: 8498

Answers (1)

emesday
emesday

Reputation: 6186

The type of x_0[0] is still numpy.matrixlib.defmatrix.matrix, not a scalar value. You need get a scale value to treat as a matrix element. Try this code

test = J(x_0.item(0), x_0.item(1))

Upvotes: 1

Related Questions