addition to MLab: randperm

les ander les_ander at yahoo.com
Thu Jan 17 13:05:13 EST 2002


I needed the matlabs randperm function so i wrote one and 
wanted to see if there is a better way. I think this function would
be a good addition to MLab:

# given a number n, return an array that is a shuffling of 
# integers from range 0 to n 

def randperm(n):
  import MLab,Numeric
  r=MLab.rand(n)
  dict={}
  for i in range(n):
     dict[ r[i] ]=i
  r=Numeric.sort(r)
  out=Numeric.zeros(n)
  for i in range(n):
     out[i]=dict[r[i]]
  return out

i encourage other people to post the code if they write
more matlab type functions using the Numeric package



More information about the Python-list mailing list