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

Keith Goodman kwgoodman@gmail....
Fri May 9 13:41:08 CDT 2008


On Fri, May 9, 2008 at 11:23 AM, Robert Kern <robert.kern@gmail.com> wrote:
> 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.

That looks good. But at the end of the function I'll have to convert
back to a 1d array if the input is 1d

np.whence_you_came_from(x)

I guess there is no way to not test for the shape.


More information about the Numpy-discussion mailing list