Coding Example: How to find if one vector is view of the other?
As a conclusion to this chapter, we'll look at one simple case study to find if one vector is view of the other.
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:
Press + to interact
import numpy as npZ1 = np.arange(10) #store a numpy array in `Z1` of size 10 containing values from 1-10Z2 = Z1[1:-1:2] #store values of alternating indices of Z1 in Z2print(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 │
╌╌╌╌╌╌╌┴───┴╌╌╌┴───┴╌╌╌┴───┴╌╌╌┴───┴╌╌╌╌╌╌╌╌╌╌