[SciPy-User] How to select multiple columns at the same time?

Keith Goodman kwgoodman@gmail....
Mon Oct 25 23:02:31 CDT 2010


On Mon, Oct 25, 2010 at 8:46 PM, 22Nick22 Yanpei Liu <yliu73@wisc.edu> wrote:

> Suppose A is an n * m matrix. I want to extract its first b columns just like Matlab command A(1:n, 1:b). What's the best way to do that?

Make an array:

>> import numpy as np
>> a = np.arange(12).reshape(3,4)
>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

Extract first b columns:

>> b = 2
>> a[:,:b]
array([[0, 1],
       [4, 5],
       [8, 9]])

Extract first b columns and first n rows:

>> n = 2
>> a[:n,:b]
array([[0, 1],
       [4, 5]])


More information about the SciPy-User mailing list