How do I iterate over a numpy array of n dimensions and create a new array of similar shape.
e.g. for the inputs:
import numpy as np
arr = np.array([[1,2,3],
[4,5,6],
[7,8,9],
[0,0,0]])
alpha = 3.
median = np.median(arr)
I would like to build a new array of same (4,3) with flags set to 1 for a random condition. e.g.
flag = (arr[i,j] > median - alpha) or (arr[i,j] < median + alpha)
I would solve this with 2 for statements
flags = arr * 0
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
flags[i,j] = (arr[i,j] > median - alpha) or (arr[i,j] < median + alpha)
Is there a way to solve this in a simpler and more efficient pythonic way ? The solution should ideally also work for n dimensional arrays (1,2, ... n dimensions)
Trueflags unlessarr[i,j] == median * alpha. Is that what you want?