I have an array a
of numbers.
rnd = np.random.default_rng(12345)
a = rnd.uniform(0, -50, 5)
# array([-11.36680112, -15.83791699, -39.86827287, -33.81273354,
# -19.55547753])
I want to find difference of the array with each element in the same array. The example output would be:
[array([ 0. , 4.47111586, 28.50147174, 22.44593241, 8.18867641]),
array([-4.47111586, 0. , 24.03035588, 17.97481655, 3.71756054]),
array([-28.50147174, -24.03035588, 0. , -6.05553933,
-20.31279534]),
array([-22.44593241, -17.97481655, 6.05553933, 0. ,
-14.25725601]),
array([-8.18867641, -3.71756054, 20.31279534, 14.25725601, 0. ])]
My first approach was to use a list comprehension [i - a for i in a]
. However, since my original array a
is quite huge, and I have thousands of such a
s where I need to do the same operation, the overall process becomes quite slow and memory hungry to the point where jupyter kernel dies.
Is there any possible way where I could speed up this?