python - Removing every other element in NumPy -
i can't life of me figure out.
i'm trying remove every other element in second axis of array. did in matlab arr(:,:,2:2:end) = [];, when tried same in python , compare 2 outputs, different matrix.
i've tried arr = np.delete(arr,np.arange(0,arr.shape[2],2),2) , arr = arr[:,:,1::2], neither seem come matlab.
example:
matlab
disp(['before: ',str(arr[21,32,11])]) arr(:,:,2:2:end) = []; disp(['after: ',str(arr[21,32,11])]) output:
before: 99089 after: 65699 python
print 'before: ' + str(arr[20,31,10]) arr = arr[:,:,1::2] # same output np.delete(arr,np.arange(0,arr.shape[2],2),2) print 'after: ' + str(arr[20,31,10]) output:
before: 99089 after: 62360 i hope i'm not overlooking fundamental.
you trying delete every other element starting second element onwards in last axis. in other words, trying keep every other element starting first element onwards in axis.
thus, working other way around of selecting elements instead of deleting elements, matlab code arr(:,:,2:2:end) = [] equivalent (neglecting performance numbers) :
arr = arr(:,:,1:2:end) in python/numpy, be:
arr = arr[:,:,0::2] or simply:
arr = arr[:,:,::2]
Comments
Post a Comment