As the title suggests I am trying to multiply each value in a 2d array by corresponding values in another 2d array. I can do this and have written the following code for it. However my issue is it takes too long as each 2d array contains 1000 arrays which contains 15289 numbers. And I have to do this three times as I have three 2d arrays like this. Currently it is taking a minute to do all of them (roughly 20 seconds to run the following code). This is far too long as I have 100 sets of data to run through my entire script which each contain 3 lots of these 2d arrays. If I can cut this 20 seconds down it would save me a lot of time in the long run as everything else runs smoothly!
e_data = [[i*j for i,j in y] for y in np.dstack((e_data,sens_function))]
e_data
is my radio flux values (for any radio astronomers out there) and sens_function
is the other array in the multiplication (this will get my e_data
to the units I require). Any help or advice would be very much appreciated!
I think you are overcomplicating with using nested for
loops and dstack
. You can just use the *
(multiplication) operator. For 2d arrays, it will perform element wise multiplication. See the following example:
e_data = np.arange(9).reshape(3,3)
print (arr1)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
sens_function = np.arange(9).reshape(3,3)
print (arr2)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
result = e_data*sens_function
print (result)
# [[ 0 1 4]
# [ 9 16 25]
# [36 49 64]]