[Numpy-discussion] About Reshape()

andrea_gavana at tin.it andrea_gavana at tin.it
Wed May 11 09:43:09 EDT 2005


Hello NG,

    I am not really a guru with Numeric, and I am trying to port some Matlab
code to Python, using Numeric. The code is working, but it is slown down
because of the different implementation (ordering) of reshape() in Matlab
and Numeric.

For example, in Matlab I can do:

a = 1:10;
reshape(a,5,2)

ans =

     1     6
     2     7
     3     8
     4     9
     5    10


In Numeric:

a = range(1, 11)
Numeric.reshape(a, (5,2))

array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])

Hmm... let's try again...

Numeric.reshape(a, (2,5))

array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])

Hmm... the only solution I found:

Numeric.transpose(Numeric.reshape(a, (2,5)))

array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])

This continuous use of transpose() is killing the performances of my scripts...
is there a workaround? Am I missing something so obvious?

Thank you for every suggestion.

Andrea.





More information about the NumPy-Discussion mailing list