I have an array and I want to loop through its values to update it as follows:
import numpy as np
arr=np.ones((5,7))
for i in range(1,arr.shape[0]-1):
for j in range(1,arr.shape[1]-1):
arr[i,j]=arr[i+1,j]+arr[i,j+1]
The result is, as desired,
[[1. 1. 1. 1. 1. 1. 1.]
[1. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 1.]
[1. 1. 1. 1. 1. 1. 1.]]
However, for-loops are quite slow and I'd like to know if there is a way to make this more efficient.
Edit: The input is not always np.ones((5,7)), it will be something more heterogeneous in general.
arr=np.arange(5 * 7).reshape(5,7)?