2

First off, apologies if this is a clear duplicate. Just getting started with numpy and I totally lack the language to describe my problem properly. Having said that, I'm looking for the correct way to do this in numpy.

I'm trying to multiply a list of ints against a corresponding row in a 2D array in numpy. The 2D array is a 10x10 checkerboard-like array, built like this:

import numpy as np

# build 10x10 array of zeros
a = np.array([[0]*10]*10)

# checkerboard out the array
a[::2,::2] = 1
a[1::2,1::2] = 1

# Result (so far so good - look at that checkerboard! Woohoo!)
print(a)
[[1 0 1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1 0 1]]

Now, I want to build my range of ints to multiply against my 2D checkerboard:

b = np.array(range(5,15))
print(b)
[ 5  6  7  8  9 10 11 12 13 14]

Looks range-y to me and 10 values long - perfect! Now, in order to solve this, I've enumerated and looped through b (my list of ints), and manually multiplied the corresponding row against the int like this:

for i, n in enumerate(b):
    a[i] = a[i] * n

print(a)
[[ 5  0  5  0  5  0  5  0  5  0]
 [ 0  6  0  6  0  6  0  6  0  6]
 [ 7  0  7  0  7  0  7  0  7  0]
 [ 0  8  0  8  0  8  0  8  0  8]
 [ 9  0  9  0  9  0  9  0  9  0]
 [ 0 10  0 10  0 10  0 10  0 10]
 [11  0 11  0 11  0 11  0 11  0]
 [ 0 12  0 12  0 12  0 12  0 12]
 [13  0 13  0 13  0 13  0 13  0]
 [ 0 14  0 14  0 14  0 14  0 14]]

Nice! While this is what I want to achieve and it works, after having seen the power numpy has in working with arrays like this, is this the preferred numpy way?

4
  • The preferred numpy way would be: a * b[:, None]. Also note that for creating b you could do: np.arange(5,15) Commented Dec 6, 2019 at 22:08
  • @RaySteam yup! Want to answer it so I can accept your solution? Thanks! Commented Dec 6, 2019 at 22:11
  • I'm cool, thanks. Note that if your code works this is better posted on codereview.stackexchange.com Commented Dec 6, 2019 at 22:15
  • 1
    You're the man! Thanks @RaySteam =] Commented Dec 6, 2019 at 22:17

1 Answer 1

1

Solution 1:

(a*b).T

a*b will initially multiply all columns by the corresponding element in b, and then you just need the transpose.

array([[ 5,  0,  5,  0,  5,  0,  5,  0,  5,  0],
       [ 0,  6,  0,  6,  0,  6,  0,  6,  0,  6],
       [ 7,  0,  7,  0,  7,  0,  7,  0,  7,  0],
       [ 0,  8,  0,  8,  0,  8,  0,  8,  0,  8],
       [ 9,  0,  9,  0,  9,  0,  9,  0,  9,  0],
       [ 0, 10,  0, 10,  0, 10,  0, 10,  0, 10],
       [11,  0, 11,  0, 11,  0, 11,  0, 11,  0],
       [ 0, 12,  0, 12,  0, 12,  0, 12,  0, 12],
       [13,  0, 13,  0, 13,  0, 13,  0, 13,  0],
       [ 0, 14,  0, 14,  0, 14,  0, 14,  0, 14]])

Solution 2:

b = b.reshape(-1,1) # make it colum vector
a*b

returns

array([[ 5,  0,  5,  0,  5,  0,  5,  0,  5,  0],
       [ 0,  6,  0,  6,  0,  6,  0,  6,  0,  6],
       [ 7,  0,  7,  0,  7,  0,  7,  0,  7,  0],
       [ 0,  8,  0,  8,  0,  8,  0,  8,  0,  8],
       [ 9,  0,  9,  0,  9,  0,  9,  0,  9,  0],
       [ 0, 10,  0, 10,  0, 10,  0, 10,  0, 10],
       [11,  0, 11,  0, 11,  0, 11,  0, 11,  0],
       [ 0, 12,  0, 12,  0, 12,  0, 12,  0, 12],
       [13,  0, 13,  0, 13,  0, 13,  0, 13,  0],
       [ 0, 14,  0, 14,  0, 14,  0, 14,  0, 14]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @makis! I appreciate you explaining some of that for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.