I'm looking for a numpy solution for generating a new (n+1)-dimensional array (3D "morph" in the example below) consisting of a number of n-dim intermediate arrays between an initial array and a final array (2D "start" and "end" in the example below).
I have tried the following:
import numpy as np
start = np.arange(6).reshape(2, 3)
end = np.array([18, 10, 17, 15, 10, 2]).reshape(2, 3)
print(start)
print(end)
Giving us a start and an end array: in this case for start:
[[0 1 2]
[3 4 5]]
and for end:
[[18 10 17]
[15 10 2]]
The gradual transforming array can be generated as follows:
morph = np.zeros((4, start.shape[0], (start.shape[1]))) # adding 2 arrays in between
for i in np.arange(start.shape[0]): # for each row
for j in np.arange(start.shape[1]): # for each column
morph[:, i, j] = np.linspace(start[i, j], end[i, j], 4)
# calculating array of intermediate values
print(morph)
and giving us the desired output:
[[[ 0. 1. 2.]
[ 3. 4. 5.]]
[[ 6. 4. 7.]
[ 7. 6. 4.]]
[[ 12. 7. 12.]
[ 11. 8. 3.]]
[[ 18. 10. 17.]
[ 15. 10. 2.]]]
Is there a better (faster) way with a numpy routine doing this without the need of having 2 loops? I was hoping for something like (generating a new linear spaced array between start and end):
morph = np.linspace(start.all(), end.all(), 4)
But then I get:
[ 0. 0.33333333 0.66666667 1. ]
which is not the intention, nor the desired outcome.
Thank you for your time!