Array or matrix access and manipulation could be confusing for the beginner. The default python array access is by reference, and the
copy routine has to be used if you need do a hard copy (copy value only). Please see the following code for the demonstration:
- Code: Select all
>>> from pylab import *
>>> a = np.arange(1,10)
>>> print(a)
[1 2 3 4 5 6 7 8 9]
>>> ar = a # assign by reference
>>> ar[0] = 10 # now change the first element value of array ar
>>> print(a)
[10 2 3 4 5 6 7 8 9]
>>>
>>> a = np.arange(1,10) # re-initiate the array
>>> print(a)
[1 2 3 4 5 6 7 8 9]
>>> av = a.copy() # hard copy
>>> av[0] = 10 # now change the first element value of array av
>>> print(a)
[1 2 3 4 5 6 7 8 9]