Search⌘ K
AI Features

Coding Example: How to find if one vector is view of the other?

Explore how to identify if one NumPy vector is a view of another by analyzing array memory layout. Learn to use strides and byte bounds properties to find slicing parameters and validate results with NumPy methods.

Problem Statement

Given two vectors Z1 and Z2. We would like to know if Z2 is a view of Z1 and if yes, what is this view?

Example

Given below is a running example to give you a better understanding:

Python 3.5
import numpy as np
Z1 = np.arange(10) #store a numpy array in `Z1` of size 10 containing values from 1-10
Z2 = Z1[1:-1:2] #store values of alternating indices of Z1 in Z2
print(Z1)
print(Z2)

Illustration

The below illustration shows what the two vectors Z1 and Z2 would look like:

  
      ╌╌╌┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬╌╌
   Z1    │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │
      ╌╌╌┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴╌╌
      ╌╌╌╌╌╌╌┬───┬╌╌╌┬───┬╌╌╌┬───┬╌╌╌┬───┬╌╌╌╌╌╌╌╌╌╌
   Z2        │ 1 │   │ 3 │   │ 5 │   │ 7 │
      ╌╌╌╌╌╌╌┴───┴╌╌╌┴───┴╌╌╌┴───┴╌╌╌┴───┴╌╌╌╌╌╌╌╌╌╌

Step 1: Check if view

...