Problem

4/12

Other Ways to Create Arrays and Matrices

Theory Click to read/hide

Ways to create arrays and matrices

Other useful ways to create arrays and matrices.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
eleven
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
34
35
36
37
import numpy as np # One-dimensional array of zeros print(np.zero(5)) #[0. 0.0.0.0.] # Two-dimensional array of zeros print(np.zeros((2, 3))) # [[0. 0.0.] #[0. 0.0.]] # 3D array of units print(np.ones((2,3,4))) # [[[1. 1. 1. 1.] # [1. 1. 1. 1.] # [1. 1. 1. 1.]] # # [[1. 1. 1. 1.] # [1. 1. 1. 1.] # [1. 1. 1. 1.]]] # Array of zeros with type indication print(np.zeros(5, dtype=np.int)) # [0 0 0 0 0] # An array based on a list of lists print(np.array([[1,2.0],[0,0],(1,3.)])) # [[1. 2.] #[0. 0.] # [1. 3.]] # An array filled with elements of an arithmetic progression starting from 0 print(np.arange(10)) # [0 1 2 3 4 5 6 7 8 9] # Arithmetic progression with type indication print(np.arange(2, 10, dtype=np.float)) # [2. 3. 4. 5. 6. 7. 8. 9.] # Arithmetic progression with non-integer difference print(np.arange(2, 3, 0.1)) # [2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9] # Arithmetic progression with a given number of terms print(np.linspace(1., 4., 6)) # [1. 1.6 2.2 2.8 3.4 4. ]

Problem

1. Read from the keyboard the number n.
2. In the variable V NumPy , create a vector containing n zeros of type int.