Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [numpy]add a value to an array
#1
I have an array created with numpy

arr10=np.random.randint(0,10,(4,4))
If for example I want to sum to each element the value 10 where the value is > 5
how I do that please ?
Thank You -
Yoriz wrote May-23-2019, 06:28 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
Quote:I want to sum to each element the value 10

I don't understand this part. Would you like to add 10 to every value over 5? In such case you could do something like this:

>>> import numpy as np
>>> arr10=np.random.randint(0,10,(4,4))
>>> arr10
array([[7, 2, 6, 2],
       [5, 7, 0, 8],
       [4, 2, 0, 5],
       [1, 5, 6, 6]])
>>> np.where(arr10 > 5, arr10+10, arr10)
array([[17,  2, 16,  2],
       [ 5, 17,  0, 18],
       [ 4,  2,  0,  5],
       [ 1,  5, 16, 16]])
>>>
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  memory issue loading movie to numpy array djf123 1 49 Nov-07-2019, 06:19 AM
Last Post: ThomasL
  Numpy structured array Nitram 3 220 Sep-17-2019, 10:09 PM
Last Post: jefsummers
  numpy.copy / numpy.delete paul18fr 3 268 Jul-26-2019, 01:51 PM
Last Post: paul18fr
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 4,180 May-29-2019, 02:48 AM
Last Post: heiner55
  Array in numpy Truman 5 4,113 May-26-2019, 10:14 AM
Last Post: snippsat
  [split] Array in numpy aapurdel 1 282 May-25-2019, 08:05 PM
Last Post: buran
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 406 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  while creating a array using numpy.arrange, how to negate only the last 50 numbers? vicky53 1 408 Apr-06-2019, 11:52 AM
Last Post: scidam
  How to get the size of a numpy array? mcgrim 2 457 Mar-23-2019, 02:25 PM
Last Post: perfringo
  creating an empty NumPy array in Python sandeepatel 2 1,081 Jan-16-2019, 09:24 AM
Last Post: rinu

Forum Jump:


Users browsing this thread: 1 Guest(s)