Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Replacing sub array in Numpy array
#1
I'm trying to replace a sub array in a Numpy array, with an array of the same shape, such that any changes are mirrored in both arrays. I've run the following code in IDLE.

>>> import numpy
>>> a=numpy.zeros((2,1))
>>> a
array([[0.],
       [0.]])
>>> b=numpy.zeros((1))
>>> b
array([0.])
>>> a[0]=b
>>> b[0]=1
>>> b
array([1.])
>>> 
Now what I'd want the output of a to be in this example is:

array([[1.],
       [0.]])
but instead I get:

[b]array([[0.],
       [0.]])[/b]
I've been trying to read up on slicing and indexing, but it's not immediately obvious to me what I'm doing wrong here, or if it's even possible to get the result I want. So I was hoping someone could tell me how, if at all, I can do this.
Quote
#2
You cann't do this with default dtype (np.float64). However, numpy arrays are mutable,
so, if you define a-array with dtype= np.object,everything should work fine.
Try the following example:
a = np.array([1,3,4], dtype=np.object)
b = np.array([0])
a[0] = b
print(a)
b[0] = 99
print(a)
Quote
#3
(Mar-18-2020, 10:06 AM)scidam Wrote: You cann't do this with default dtype (np.float64). However, numpy arrays are mutable,
so, if you define a-array with dtype= np.object,everything should work fine.
Try the following example:
a = np.array([1,3,4], dtype=np.object)
b = np.array([0])
a[0] = b
print(a)
b[0] = 99
print(a)

But my example code uses multidimensional arrays. If I try that with your code, it doesn't work anymore.
Quote
#4
And nevermind my previous post. It took me a while but I finally figured out how it works and actually got some similar code to run. Now it's on to multidimensional arrays.
Quote
#5
(Mar-30-2020, 07:24 PM)ThemePark Wrote: Now it's on to multidimensional arrays.
The same rules are true for multidimensional cases.
import numpy as np
x=np.ones((2,2,2), dtype=np.float32)
z =  np.zeros((2,2,2), dtype=np.object)
z[0][0][0] = x
x[0][0][0] = 99
print(z)
Quote
#6
(Apr-01-2020, 01:52 AM)scidam Wrote:
(Mar-30-2020, 07:24 PM)ThemePark Wrote: Now it's on to multidimensional arrays.
The same rules are true for multidimensional cases.
import numpy as np
x=np.ones((2,2,2), dtype=np.float32)
z =  np.zeros((2,2,2), dtype=np.object)
z[0][0][0] = x
x[0][0][0] = 99
print(z)

Okay, truthfully I didn't realize you'd go that way. But what I mean by multidimensional arrays is that z[0][0][0] would not contain x, but x[0][0][0] and then the 99 would be shown in both arrays.

You can look at my other post too, it's the same problem I'm trying to solve.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  array coding problem pberrett 3 208 May-08-2020, 05:54 AM
Last Post: nnk
  ValueError: Found array with 0 samples marcellam 1 186 Apr-22-2020, 04:12 PM
Last Post: jefsummers
  np.array question hissonrr 3 244 Apr-20-2020, 01:15 PM
Last Post: hissonrr
  Create multiple quiver plots 3D array PengEng 0 169 Apr-14-2020, 12:57 PM
Last Post: PengEng
  Read json array data by pandas vipinct 0 151 Apr-13-2020, 02:24 PM
Last Post: vipinct
  define certain array elements+display with digits lukezo 0 182 Apr-10-2020, 05:03 PM
Last Post: lukezo
  Creating look up table/matrix from 3d data array chai0404 3 235 Apr-09-2020, 04:53 AM
Last Post: buran
  converting dataframe to int numpy array glennford49 1 248 Apr-04-2020, 06:15 AM
Last Post: snippsat
  Inserting slice of array objects into different slice ThemePark 4 284 Apr-01-2020, 01:10 PM
Last Post: ThemePark
  sequential sampling from an array to a matrix nsansen 1 235 Mar-29-2020, 08:15 PM
Last Post: deanhystad

Forum Jump:


Users browsing this thread: 1 Guest(s)