0

Python中的3D矩阵操作

 9 months ago
source link: https://xugaoxiang.com/2023/09/20/python-3d-matrix/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python中的3D矩阵操作 - 迷途小书童的Note迷途小书童的Note

3D 矩阵又称为立体矩阵,是指一个具有三个维度的矩阵结构。相比二维矩阵,它增加了一个深度维度。在 3D 矩阵中,第一个维度表示行数,第二个维度表示列数,第三个维度表示层数或深度,可以想象成一个多层的立方体结构。三维矩阵通常也称为 NxNxN 矩阵,在计算机视觉、医学成像、深度学习、增强现实等各个领域和应用中都非常有用。

本文中,我们将逐步介绍在 Python 中如何实现和使用 3D 矩阵,这里会介绍2种方法

  • 使用python中的列表
  • 使用numpy库

使用嵌套列表来实现 3D 矩阵是相对容易想到的方法。我们将从一个空列表开始,并向其中添加更多列表以表示 3D 矩阵的每一层。在此 3D 矩阵的每一层中,我们将附加列表来表示行,并在每行中添加各个元素。这样,我们将构建一个列表层次结构,模拟矩阵的三个维度。通过遍历列表的层次结构,我们可以访问和修改矩阵中的各个部分。

比如,我们要创建一个全是零的 3D 矩阵



  1. N = 3
  2. three_d_matrix = [[[0 for _ in range(N)] for _ in range(N)] for _ in range(N)]
  3. print(three_d_matrix)

这行代码使用嵌套列表推导式创建一个全是0的 3D 矩阵。每个 for _ in range(N) 循环都会创建一个包含 N 个零的一维列表。这里,_ 是当我们不需要循环变量时使用的常见约定。

代码的执行的结果是



  1. [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

要访问和修改 3D 矩阵中的元素,我们可以这样做



  1. # 给元素赋值
  2. three_d_matrix[0][1][2] = 33

要打印整个 3D 矩阵



  1. for layer in three_d_matrix:
  2. for row in layer:
  3. print(row)
  4. print("-" * 10)

打印语句的输出是



  1. [0,0,0]
  2. [0,0,33]
  3. [0,0,0]
  4. ----------
  5. [0,0,0]
  6. [0,0,0]
  7. [0,0,0]
  8. ----------
  9. [0,0,0]
  10. [0,0,0]
  11. [0,0,0]
  12. ----------

使用NumPy实现

NumPy 是一个用于数值和科学计算的 Pyhton 库,它包含广泛的内置功能,可用于各种用途。要使用 Numpy,首先要做的就是安装 NumPy 库,执行命令



  1. pip install numpy

下面我们来看看如何实现



  1. import numpy as np
  2. N = 3
  3. three_d_matrix = np.zeros((N, N, N), dtype=int)
  4. print(three_d_matrix)

这里通过 np.zeros() 来创建一个全是0的 3D 数组,其中 (N, N, N) 参数指定数组的形状,dtype=int 指定数组元素的数据类型。

代码执行的结果是



  1. [[[0 0 0]
  2. [0 0 0]
  3. [0 0 0]]
  4. [[0 0 0]
  5. [0 0 0]
  6. [0 0 0]]
  7. [[0 0 0]
  8. [0 0 0]
  9. [0 0 0]]]

访问和修改元素



  1. three_d_matrix[0, 1, 2] = 33

要迭代 3D 矩阵中的每个元素,我们需要借助嵌套 for 循环



  1. import numpy as np
  2. N = 3
  3. array = np.random.rand(N, N, N)
  4. for i in range(N):
  5. for j in range(N):
  6. for k in range(N):
  7. element = array[i, j, k]
  8. print(f"Element at ({i}, {j}, {k}) = {element}")


  1. import numpy as np
  2. N = 3
  3. array1 = np.random.rand(N, N, N)
  4. array2 = np.random.rand(N, N, N)
  5. result = array1 + array2
  6. print(result)

类似地,对于减法



  1. import numpy as np
  2. N = 3
  3. array1 = np.random.rand(N, N, N)
  4. array2 = np.random.rand(N, N, N)
  5. result = array1 - array2
  6. print(result)

然后就是乘法



  1. import numpy as np
  2. N = 3
  3. array1 = np.random.rand(N, N, N)
  4. array2 = np.random.rand(N, N, N)
  5. result = np.matmul(array1, array2)
  6. print(result)


  1. import numpy as np
  2. N = 3
  3. array = np.random.rand(N, N, N)
  4. transposed_array = array.transpose()
  5. print("Original Array:")
  6. print(array)
  7. print("\nTransposed Array:")
  8. print(transposed_array)

transpose() 是一个内置的 NumPy 函数,用于执行 3D 数组的转置。矩阵的转置是一个新的矩阵,其中的行变成列,列变成行。

通过实现代码的对比,我们可以清楚地看到,2种方法中,使用 NumPy 可以更快地执行数值计算,并且具有更丰富的功能,效率也更高。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK