I found multiple questions with almost the same title but none of them have a working code.
so I have the following 1D array a
, once I reshape it to 3d array b
, and then reshape it back to 1D array.
import numpy as np
depth = 2
row = 3
col = 4
a = np.arange(24)
b = a.reshape((depth, row, col))
c = np.zeros(row*col*depth)
print(a)
print(b)
index = 0
for i in range(depth):
for j in range(row):
for k in range(col):
c[k+ j * row + i * (col*row)] = b[i][j][k] # ???
# c[k + row * (j + depth * i)] = b[i][j][k]
# c[index] = b[i][j][k]
# index+=1
print(c)
# [ 0. 1. 2. 4. 5. 6. 8. 9. 10. 11. 0. 0. 12. 13. 14. 16. 17. 18.
# 20. 21. 22. 23. 0. 0.]
I don't get Flat[x + WIDTH * (y + DEPTH * z)] = Original[x, y, z]
in this case.
EDIT
I am using this code (the equivalent) in a CUDA code, so I need the loops. I am not going to use any numpy magic functions.