I want to change a value of given array in numpy to a multiplication of other elements of the array. Therefore I want to extract the multi_index and manipulate it so that I can identify the position and use it. (e.g. nditer through all elements and always do 'current position in array = next position +position above in array'
I tried to call a function with the multi_index of the current position and want said function to take it and e.g. increase it by one position. (<0 , 1> ---> <0 , 2> while <0 , n> n>=length otherwise <0 , 1> ---> <1 , 0>)
import numpy as np;
def fill(multi_index):
"This will get the new value of the current iteration value judgeing from its index"
return a[(multi_index + <0,1>) + (multi_index + <0,-1>) + (multi_index + <1,0>) + (multi_index + <-1,0>)]
#a = np.random.uniform(0, 100, size=(100, 100))
a = np.arange(6).reshape(2,3)
it = np.nditer(a, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
it[0] = fill(it.multi_index)
print(it[0])
it.iternext()
"""for x in np.nditer(a, flags=['multi_index'], op_flags=['readwrite']):
print(x)"""
I don't understand how to extract the actual "coordinates" from the multi_index. I am kinda new to python so please try to explain it thoroughly if possible. Thanks.
Edit: Before I only coded on C++ and a bit Java, so I used to mainly using arrays (in c++ it would be somthing like this:
int main() {
int a[100][100];
for (int i=1, i<=a.length-1, i++) {
for (int j=1, i<=a.width-1, j++) {
a[i][j] = 1/4 (a[i][j+1]+a[i][j-1]+a[i+1][j]+a[i-1][j]);
}
}
return 0;
}