Python Forum
Replacing sub array in Numpy array
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.
Reply


Messages In This Thread
Replacing sub array in Numpy array - by ThemePark - Mar-18-2020, 04:19 AM
RE: Replacing sub array in Numpy array - by scidam - Mar-18-2020, 10:06 AM
RE: Replacing sub array in Numpy array - by scidam - Apr-01-2020, 01:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ndim array Sowmya 2 407 Apr-03-2024, 04:19 AM
Last Post: Sowmya
  [Numpy] How to store different data type in one numpy array? water 7 894 Mar-26-2024, 02:18 PM
Last Post: snippsat
  boolean array: looking for all rows where all is True paul18fr 4 1,302 Jan-04-2023, 09:58 PM
Last Post: paul18fr
  reshaping 2D numpy array paul18fr 3 1,111 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Turn list of arrays into an array of lists Cola_Reb 6 1,801 Jul-20-2022, 06:55 PM
Last Post: Cola_Reb
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,767 Jul-19-2022, 06:31 AM
Last Post: paul18fr
Question how to write a function that accepts a 1D array as a parameter in Python SuperNinja3I3 1 1,700 Jul-02-2022, 01:55 PM
Last Post: Larz60+
  replace sets of values in an array without using loops paul18fr 7 1,863 Jun-20-2022, 08:15 PM
Last Post: paul18fr
  how to parse this array with pandas? netanelst 1 1,431 May-17-2022, 12:42 PM
Last Post: netanelst
  semantics of comma inside array brackets usercat123 2 1,432 Apr-23-2022, 09:08 AM
Last Post: usercat123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020