-6

I have a trouble with python broadcasting

there is two numpy list

x = np.array([[1,2,3],[4,5,6]])

y = np.array([0,1,1])

I'd like to calculate

x : [[1,2,3],
     [4,5,6]]

to

x : [[1-y[0], 2-y[1], 3-y[2]],
     [4-y[0], 5-y[1], 6-y[2]]]

that is,

x : [[0,-3,-3],
     [3,0,0]] 

at one time.

Please, let me know how to do that.

I'm sorry...i had a mistake....

i mean x : [[1,1,2], [4,4,5]]

Cœur
  • 37,241
  • 25
  • 195
  • 267
yi han
  • 1
  • 1

1 Answers1

2

I don't see how you got:

x : [[0,-3,-3],
     [3,0,0]] 

But the easiest way to do what you ask for is just:

import numpy as np
X = ([1,2,3],[4,5,6])
y = np.array([0,1,1])
print X-y

Output:

[ [1 1 2]
[4 4 5] ]

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40