I am trying to implement a NaN-safe shuffling procedure in Cython, which can move along several axes of a multidimensional matrix of arbitrary dimension.
In the simple case, 1D matrices can simply be shuffled for all indices with values ββother than NaN using the Fisher-Yeiss algorithm:
def shuffle1D(np.ndarray[double, ndim=1] x): cdef np.ndarray[long, ndim=1] idx = np.where(~np.isnan(x))[0] cdef unsigned int i,j,n,m randint = np.random.randint for i in xrange(len(idx)-1, 0, -1): j = randint(i+1) n,m = idx[i], idx[j] x[n], x[m] = x[m], x[n]
I would like to extend this algorithm to handle large multidimensional arrays without modification (which launches a copy for more complex cases that are not covered here). To do this, I need to get rid of a fixed input dimension, which is not possible using numpy arrays or memory in Cython. Is there a workaround?
Thank you very much in advance!
source share