i love stackoverflow
i love stackoverflow

Reputation: 1685

Is it possible to create a numpy matrix with 10 rows and 0 columns?

My objective is to start with an "empty" matrix and repeatedly add columns to it until I have a large matrix.

Upvotes: 1

Views: 432

Answers (2)

HYRY
HYRY

Reputation: 97331

Add columns to ndarray(or matrix) need full copy of the content, so you should use other method such as list or the array module, or create a large matrix first, and fill data in it.

Upvotes: 2

Danica
Danica

Reputation: 28856

Yes:

>>> a = np.zeros((10, 0))
>>> a.shape
(10, 0)

You can then use ndarray.resize to expand it after the fact without copying. This has some problems though, and for many applications it'll be easier to use a list of vectors that you then merge into a single array.

Upvotes: 3

Related Questions