[Numpy-discussion] A matrix user in the land of arrays

Robert Kern robert.kern@gmail....
Fri May 9 13:23:59 CDT 2008


On Fri, May 9, 2008 at 12:52 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> The recently proposed changes to the matrix class was the final push I
> needed to begin slowly porting my package from matrices to arrays. But
> I'm already stuck in the first stage (all new modules must use
> arrays).
>
> Here's a toy example of iterating over columns of a matrix:
>
> x is a nxm matrix
> y is a nx1 matrix
>
> for j in xrange(x.shape[1]):
>    idx = where(y > scalar)[0]
>    x[idx,j] = scalar
>
> If x and y become 2d arrays, the code still works. But what confuses
> me is how to generalize it to work for both 2d and 1d arrays.

Use atleast_2d(x) to get a 1xm array, then use your 2D code on it.

-- 
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 Numpy-discussion mailing list