I have a nested loop where I calculate 10 distances, and store them in an array b_array
. Then into a_array
another array which will is to keep the results for every image. The problem is that my code overwrites a_array
for every iteration even when it is outside the main function when I checked with print
. Therefore, I end up with the exact same result of the last executed file in the loop for all 4 images.
JavaScript
x
10
10
1
distances = np.linspace(min_distance, max_distance, 10)
2
a_array = np.zeros((4,10))
3
4
for x in range (0, 4):
5
b_array = np.zeros(10)
6
for y in range(0,10):
7
b_array[y] = mean_intensity(distances)#local function
8
a_array[x] = b_array
9
print(a_array)
10
Advertisement
Answer
If you want a (4,10) array with each row as evenly spaced numbers, then you could do: np.linspace([min_distance]*4, [max_distance]*4, 10, axis=1)
, which would have the same effect as:
JavaScript
1
8
1
min_distance = 0
2
max_distance = 10
3
a_array = np.zeros((4,10))
4
5
for x in range (0, 4):
6
b_array = np.linspace(min_distance, max_distance, 10)
7
a_array[x] = b_array
8