Can a numpy 1D array not be contiguous?
Asked Answered
C

1

2

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
Collation answered 25/3, 2020 at 11:33 Comment(0)
S
5

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
Seiter answered 25/3, 2020 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.