[SciPy-user] mgrid/ogrid...

Robert Kern robert.kern@gmail....
Tue Jul 24 13:36:04 CDT 2007


fred wrote:
> Hi all,
> 
> What's difference between ogrid() and mgrid ?

In [1]: from numpy import *

In [2]: mgrid[0:5,0:5]
Out[2]:
array([[[0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3],
        [4, 4, 4, 4, 4]],

       [[0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4]]])

In [3]: ogrid[0:5,0:5]
Out[3]:
[array([[0],
       [1],
       [2],
       [3],
       [4]]),
 array([[0, 1, 2, 3, 4]])]


mgrid gives you the fully broadcasted arrays. ogrid gives you partial arrays
which will broadcast against each other. Depending on how you are using them,
ogrid's results may be more efficient to use because you can often do the bulk
of your computations on the smaller arrays and only do a handful of operations
that broadcast against each other to form the final, full result.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list