[SciPy-user] array vs matrix, converting code from matlab

Robert Kern robert.kern at gmail.com
Thu Apr 20 23:42:23 CDT 2006

David Cournapeau wrote:

> Actually, my problem is pretty similar to implementing wrapper around 
> numpy.linalg.inv which works in scalar case and rank 1 (assuming rank 1 
> means diagonal) cases. Are those atleast* functions expensive ? For 
> small size arrays, I don't care too much, but in the case of a big array 
> of rank 1 converted to a rank 2 array, does those function need to copy 
> the data ?


def atleast_2d(*arys):
    """ Force a sequence of arrays to each be at least 2D.

            Force an array to each be at least 2D.  If the array
            is 0D or 1D, the array is converted to a single
            row of values.  Otherwise, the array is unaltered.
            arys -- arrays to be converted to 2 or more dimensional array.
            input array converted to at least 2D array.
    res = []
    for ary in arys:
    if len(res) == 1:
        return res[0]
        return res

Robert Kern
robert.kern at gmail.com

"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