forked from ASPP/ASPP-2018-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-manipulation.py
More file actions
29 lines (22 loc) · 858 Bytes
/
basic-manipulation.py
File metadata and controls
29 lines (22 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -----------------------------------------------------------------------------
# Copyright (C) 2018 Nicolas P. Rougier
# Distributed under the terms of the BSD License.
# -----------------------------------------------------------------------------
import numpy as np
# Create a vector with values ranging from 10 to 49
Z = np.arange(10,50)
# Create a null vector of size 100 but the fifth value which is 1
Z = np.zeros(100)
Z[4] = 1
# Reverse a vector (first element becomes last)
Z = np.arange(50)[::-1]
# Create a 3x3 matrix with values ranging from 0 to 8
Z = np.arange(9).reshape(3,3)
# Create a 3x3 identity matrix
Z = np.eye(3)
# Create a 2d array with 1 on the border and 0 inside
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
# Given a 1D array, negate all elements which are between 3 and 8, in place
Z = np.arange(11)
Z[(3 < Z) & (Z <= 8)] *= -1