Labo
Labo

Reputation: 2772

Can a numpy 1D array not be contiguous?

Is is possible to make a 1D array not C_CONTIGUOUS or F_CONTIGUOUS in numpy?

I think the notion of contiguous only makes sense for arrays with more dimensions but I couldn't find anything in the docs.

I tried the following to make a non contiguous 1D array:

>>> np.empty(10).flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
>>> np.empty(10).copy('F').flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Upvotes: 2

Views: 374

Answers (1)

a_guest
a_guest

Reputation: 36239

Just create a view of the array that skips over some of the elements and it will be non-contiguous:

In [2]: a = np.arange(10)                                                                     

In [3]: a.flags                                                                               
Out[3]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [4]: a[::2].flags                                                                          
Out[4]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Upvotes: 5

Related Questions