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
  Error installing numpy srm 3 180 Jul-08-2019, 06:26 PM
Last Post: srm
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 904 May-29-2019, 02:48 AM
Last Post: heiner55
  Array in numpy Truman 5 3,248 May-26-2019, 10:14 AM
Last Post: snippsat
  [split] Array in numpy aapurdel 1 153 May-25-2019, 08:05 PM
Last Post: buran
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 241 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 244 Apr-06-2019, 11:52 AM
Last Post: scidam
  counting the occurence of a specified number in a numpy-matrix PhysChem 1 219 Apr-03-2019, 01:37 PM
Last Post: PhysChem
  How to get the size of a numpy array? mcgrim 2 296 Mar-23-2019, 02:25 PM
Last Post: perfringo
  How convert multidimensional array to two dimensional array tkkhan44 1 304 Feb-20-2019, 05:00 AM
Last Post: scidam
  Numpy Quick start Tutorial leodavinci1990 1 399 Feb-03-2019, 07:10 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)