Lexicographical sort for numarray

E

Edward C. Jones

Suppose arr is a two dimensional numarray array. Suppose we do the
following:

def rowsort(arr):
a = arr.tolist()
a.sort()
return numarray.array(a)

Can this sort be done efficiently in numarray? This function is called
"rowsort" in MatLab.
 
J

Jason Mobarak

import numarray as na
import random

# example array
arr = range(100)
random.shuffle(arr)
arr = na.array(arr)
arr = na.reshape(arr, (10,10))

print arr # not rowsort'ed

arr.flat.sort() # key line

print arr # rowsort'ed
 
R

Robert Kern

Jason said:
import numarray as na
import random

# example array
arr = range(100)
random.shuffle(arr)
arr = na.array(arr)
arr = na.reshape(arr, (10,10))

print arr # not rowsort'ed

arr.flat.sort() # key line

print arr # rowsort'ed

That's definitely not a lexicographic sort.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
R

Robert Kern

Jason said:
It does what the OPs example does, but with numeric types.

It certainly does not.

In [15]:arr = na.arange(100)

In [16]:random.shuffle(arr)

In [17]:arr.shape = (10,10)

In [18]:arr2 = na.array(arr)

In [19]:L = arr.tolist()

In [20]:L.sort()

In [21]:na.array(L)
Out[21]:
array([[ 8, 22, 40, 85, 64, 20, 91, 76, 19, 56],
[11, 83, 86, 51, 72, 17, 80, 35, 1, 18],
[13, 62, 0, 95, 25, 28, 5, 78, 54, 55],
[41, 44, 15, 24, 27, 97, 31, 75, 65, 29],
[45, 57, 16, 89, 87, 90, 42, 50, 93, 9],
[53, 21, 47, 69, 2, 12, 92, 98, 66, 48],
[70, 79, 36, 88, 10, 39, 81, 61, 43, 3],
[74, 37, 49, 14, 33, 34, 52, 23, 96, 71],
[82, 67, 30, 60, 6, 73, 99, 94, 7, 58],
[84, 63, 4, 46, 26, 32, 59, 38, 68, 77]])

In [22]:arr2.flat.sort()

In [23]:arr2
Out[23]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top