口数组变换与数组合并练习 1.包建一个元素为从10到/49的ndarray对象D1: 2.将D1的所有元素位置反转(矩阵转置): 3.使用np.random.randint创建一个10*10的ndarray对象,并分别打印出 最大和最小元素的坐标: 4.创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0: 5.创建一个范围在(0,1)之间的长度为12的等差数列: 6.创建一个长度为10的随机数组并排序: 7.如何将数组a =np. arange( 10) reshapel2-1)和数组b=np.repeat(1, 10).reshape(2-1)水平堆叠?
时间: 2024-02-23 12:00:46 浏览: 94
IEEE Draft P802.1r/D1
好的,我理解了你的问题。以下是答案:
1.
```python
import numpy as np
D1 = np.arange(10, 50)
print(D1)
```
2.
```python
import numpy as np
D1 = np.arange(10, 50)
D1 = D1.reshape(4, 10) # 转换为4*10的矩阵
D1 = D1.T # 矩阵转置
print(D1)
```
3.
```python
import numpy as np
D2 = np.random.randint(0, 100, (10, 10))
max_index = np.unravel_index(np.argmax(D2), D2.shape) # 最大值的坐标
min_index = np.unravel_index(np.argmin(D2), D2.shape) # 最小值的坐标
print("最大值坐标:", max_index)
print("最小值坐标:", min_index)
```
4.
```python
import numpy as np
D3 = np.zeros((10, 10))
D3[0, :] = 1
D3[-1, :] = 1
D3[:, 0] = 1
D3[:, -1] = 1
print(D3)
```
5.
```python
import numpy as np
D4 = np.linspace(0, 1, 12)
print(D4)
```
6.
```python
import numpy as np
D5 = np.random.rand(10)
D5.sort()
print(D5)
```
7.
```python
import numpy as np
a = np.arange(10).reshape(2, -1)
b = np.repeat(1, 10).reshape(2, -1)
c = np.hstack((a, b)) # 水平堆叠
print(c)
```
阅读全文