2

I have this numpy array

a=np.array([[2*y, 0],[2*x + 1, 2*y + 4]])

I want to replace (x,y) by a value, for example (1,1).

How can I have this array with this new values in order to have something like this:

a=[[2,0],[3,6]]
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
agreüs
  • 109
  • 11
  • Possible duplicate of http://stackoverflow.com/questions/34735193/change-n-th-entry-of-numpy-array-that-fulfills-condition – yasouser Apr 13 '17 at 20:27
  • How about creating a function, say `make_array(x, y)`, that returns the array `a`? – Warren Weckesser Apr 13 '17 at 20:28
  • If you are asking about modifying the array inplace, then see this question: http://stackoverflow.com/questions/10149416/numpy-modify-array-in-place – yasouser Apr 13 '17 at 20:30

1 Answers1

1

You can use SymPy (a symbolic math package) and it's Matrix (array):

>>> from sympy import Matrix
>>> from sympy.abc import x, y
>>> m = Matrix([[2*y, 0],[2*x + 1, 2*y + 4]])
>>> m
Matrix([
[    2*y,       0],
[2*x + 1, 2*y + 4]])

>>> n = m.subs({x: 1, y: 1})   # replace variables with values
>>> n
Matrix([
[2, 0],
[3, 6]])

>>> np.array(n).astype(int)    # convert to numpy array
array([[2, 0],
       [3, 6]])
MSeifert
  • 145,886
  • 38
  • 333
  • 352