Numpy subtraction from two arrays

  • A+
Category:Languages

I have two numpy arrays like below

a=np.array([11,12]) b=np.array([9])  #a-b should be [2,12] 

I want to subtract both a & b such that result should [2,12]. How can I achieve this result?

 


You can zero-pad one of the array.

import numpy as np  n = max(len(a), len(b)) a_pad = np.pad(a, (0, n - len(a)), 'constant') b_pad = np.pad(b, (0, n - len(b)), 'constant')  ans = a_pad - b_pad 

Here np.pad's second argument is (#of left pads, #of right pads)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: