numpy arrays
Heli
hemla21 at gmail.com
Wed Apr 6 12:26:12 EDT 2016
Thanks for your replies. I have a question in regard with my previous question. I have a file that contains x,y,z and a value for that coordinate on each line. Here I am giving an example of the file using a numpy array called f.
f=np.array([[1,1,1,1],
[1,1,2,2],
[1,1,3,3],
[1,2,1,4],
[1,2,2,5],
[1,2,3,6],
[1,3,1,7],
[1,3,2,8],
[1,3,3,9],
[2,1,1,10],
[2,1,2,11],
[2,1,3,12],
[2,2,1,13],
[2,2,2,14],
[2,2,3,15],
[2,3,1,16],
[2,3,2,17],
[2,3,3,18],
[3,1,1,19],
[3,1,2,20],
[3,1,3,21],
[3,2,1,22],
[3,2,2,23],
[3,2,3,24],
[3,3,1,25],
[3,3,2,26],
[3,3,3,27],
])
then after tranposing f, I get the x,y and z coordinates:
f_tranpose=f.T
x=np.sort(np.unique(f_tranpose[0]))
y=np.sort(np.unique(f_tranpose[1]))
z=np.sort(np.unique(f_tranpose[2]))
Then I will create a 3D array to put the values inside. The only way I see to do this is the following:
arr_size=x.size
val2=np.empty([3, 3,3])
for sub_arr in f:
idx = (np.abs(x-sub_arr[0])).argmin()
idy = (np.abs(y-sub_arr[1])).argmin()
idz = (np.abs(z-sub_arr[2])).argmin()
val2[idx,idy,idz]=sub_arr[3]
I know that in the example above I could simple reshape f_tranpose[3] to a three by three by three array, but in my real example the coordinates are not in order and the only way I see to do this is by looping over the whole file which takes a lot of time.
I would appreciate any workarounds to make this quicker.
Thanks,
More information about the Python-list
mailing list