[Numpy-discussion] How to Keep An Array Two Dimensional
Sun Nov 25 20:24:46 CST 2012
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion