Exercise indexing and slicing

Introduction

This notebook provides a small collection of exercises about indexing and slicing for numpy arrays. You will start working with 1D arrays working your way up to 4D. It will be always possible to choose between different styles of solving an exercise but try to use "get all (:)" or slicing if a task offers it to be more efficient and shorten your code.

Requirements

Knowledge

This notebook is the exercise part for the chapters about indexing and slicing from the notebook NumpyAndArrays, so all the knowledge you will need to solve these exercises is provided by this theory notebook.

Modules

import numpy as np

Exercises

Exercise 1D Array

A1 = np.arange(10)
print(A1)

Task: Print the first and the last element.

Task: Print each element with even index.

Task: Print the first four elements.

Task Print the last three elements.

Task Print element four to eight.

Exercise 2D Array

np.random.seed(42)
M1 = 100*np.random.rand(9,7).round(2)
print(M1)

Task Print the first and the last row with one slice operation.

Task Print all elements less than 10.

Task Print each even row.

Task Print each odd column.

Task Print the elements of the even rows and the odd columns.

Task Print all elements from the following matrix with a pair of even indices ([0,2],[2, 2], [4,2],.. etc.).

M1 = 100*np.random.rand(5,7).round(2)
print(M1)

Exercise 3D Array

np.random.seed(42)
M = np.random.randint(10, size=(2,2,10))
print(M)
# here we have four (most) internal array

Task Print the first and the last internal vector-array, i.e.

[[6 3 7 4 6 9 2 6 7 4]
 [8 2 4 2 6 4 8 6 1 3]]

Task Print the last element from each internal array.

[4 1 3 3]

Task Print every second element from the second internal array.

[3 7 5 1 5]

Task Print every element with odd index from each internal array.

[[[3 4 9 6 4]
  [7 2 4 7 1]]

 [[0 5 0 2 3]
  [2 2 4 6 3]]]
np.random.seed(42)
M = 100*np.random.rand(10,2,9).round(2)
print(M)

Exercise 4D Array

np.random.seed(42)
M = np.random.randint(0,100, size=(2,2,3,3))
print(M)

Task Print the central element from each internal matrix, i.e.

[60  2 59 58]

Task: Get the second column from each matrix, i.e.

[[[92 60 86]
  [87  2  1]]

 [[37 59 75]
  [88 58 59]]]

Task: Get the second row from each matrix, i.e.

[[[71 60 20]
  [23  2 21]]

 [[63 59 20]
  [90 58 41]]]

Task: Get every element with an even index pair from each matrix.

[[[[51 14]
   [82 74]]

  [[74 99]
   [52 87]]]
 ....

Task: Get the diagonal from each matrix.

Consider now the following 4D array.

np.random.seed(42)
M = 100*np.random.rand(6,2,6,4).round(2)
print(M)

Task Print from the second matrix of the inner matrix pair each second row.

[[[46. 79. 20. 51.]
  [ 7. 95. 97. 81.]
  [12. 50.  3. 91.]]

 [[ 1. 82. 71. 73.]
  [86. 62. 33.  6.]
  [89. 47. 12. 71.]]
....

Licenses

Notebook License (CC-BY-SA 4.0)

The following license applies to the complete notebook, including code cells. It does however not apply to any referenced external media (e.g., images).

exercise_indexing_and_slicing
Oliver Fischer
is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://gitlab.com/deep.TEACHING.

Code License (MIT)

The following license only applies to code cells of the notebook.

Copyright 2019 Oliver Fischer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.