[Numpy-discussion] How to Keep An Array Two Dimensional

Tom Bennett tom.bennett@mail.zyzhu....
Sun Nov 25 20:24:46 CST 2012


Hi,

I am trying to extract n columns from an 2D array and then operate on the
extracted columns. Below is the code:

A is an MxN 2D array.

u = A[:,:n] #extract the first n columns from A

B = np.dot(u, u.T) #take outer product.

This code works when n>1. However, when n=1, u becomes an 1D array instead
of an Mx1 2D array and the code breaks down.

I wonder if there is any way to keep u=A[:,:n] an Mxn array no matter what
value n takes. I do not want to use matrix because array is more convenient
in other places.

Thanks,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20121125/ef8bf902/attachment.html 


More information about the NumPy-Discussion mailing list