0

Example code:

import numpy as np
a = np.arange(1,11)
b = np.arange(1,11)
b[:] = 0
b[3] = 10
b[4] = 10
print(a, b)
[ 1  2  3  4  5  6  7  8  9 10] [ 0  0  0 10 10  0  0  0  0  0]

I am attempting to multiply b by element-wise a-array such that my resulting array is the following:

[0 0 10 30 50 70 90 110 130 150]

Any help would be greatly appreciated.

1
  • If what you mean is Hadamard product: en.wikipedia.org/wiki/Hadamard_product_(matrices) then you get it simply using a * b. However, for your values of a and b, the product is [ 0, 0, 0, 40, 50, 0, 0, 0, 0, 0] rather than whay you specified as expected output, so I think clarification is needed. Commented Mar 29, 2020 at 20:20

2 Answers 2

1

It looks like you want the convolution of both arrays:

np.convolve(a,b)[:len(a)+1]
# array([  0,   0,   0,  10,  30,  50,  70,  90, 110, 130, 150])
Sign up to request clarification or add additional context in comments.

1 Comment

This is correct. I was mistaken on element wise multiplication. Thank you @yatu.
0

Elementwise multiplication of b with a shall give you [0,0,40,50,0,0,0,0,0,0] and not what you have stated.

1 Comment

Yes, I was mistaken. Solution to my problem solved above using np.convolve. Thank you for responding.

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.