Exercise shaping and merging

Introduction

This notebook provides a small collection of exercises about reshaping and merging arrays.

Requirements

Knowledge

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

It would be an asset if you have done the notebook exercises_indexing_and_slicing before to be more familiar with these basic techniques.

Modules

import numpy as np

Exercises

Shaping

Task Write the following numbers into a row and a column vector. By a explicit row respectivly column vector we mean a 2D-array with the appropriate shape.

A = np.arange(1,10)
print(A)

Task Reshape the following numbers into a square matrix (3x3).

A = np.arange(1,10)
print(A)

Task Turn the matrix back into an array by deleting one dimension

Consider the following 3D array.

M = 100*np.random.rand(5,1,9).round(2)
print(M)

Task Turn this 3D array into an 2D matrix.

Task Turn the given 3D array into an array of 3x3 matrices.

Merging

Consider the following arrays.

A1 = np.arange(0,5)
A2 = np.arange(5,10)
A3 = np.arange(10,15)
A4 = np.arange(20,25)

print(A1,A2,A3,A4)

Task Merge all arrays into a single vector-array.

Task Put all arrays as rows into a matrix

Consider the following square matrix.

M = np.random.randint(0,10,size=16).reshape(4,4)
print(M)

Task Append a column of ones to the matrix as first.

Task Append a row of ones to the matrix.

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_shaping_and_merging
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.