三维数组的理解和加减乘除运算python

【大白话直观理解】

二维数组可以理解为一个坐标系,通过(x,y)来索引。

而三维数组其实就是将多个坐标系叠在一起,通过(z,x,y)来索引。

如图:A和B分别是两个二维坐标,将它们叠在一起就变成了三维坐标。而z就是来表示每个二维坐标的位置的序号,也叫“通道”。

(例:要找到a12,即先找到A(在第一通道,z=0),再找第一行第二列(x=0,y=1))

【python中的表达形式】 

首先创建一个3个通道,4行,5列的矩阵,如下:

 其中,3表示的就是每一大模块。

4和5为每一模块的行和列

如果相对某个数据进行处理,按位置依次索引即可.

如对第2大模块的第4行第5列的数据赋值为1:

 

 参考链接:(10条消息) Python中三维数组位置详解_python三维数组_bebr的博客-CSDN博客

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你实现一个自定义的三维向量类,并且重载加减乘除等运算符,以下是示例代码: ```python import math class Vector3: def __init__(self, x=0.0, y=0.0, z=0.0): self.x = x self.y = y self.z = z def __repr__(self): return f"Vector3({self.x}, {self.y}, {self.z})" def __add__(self, other): return Vector3(self.x + other.x, self.y + other.y, self.z + other.z) def __sub__(self, other): return Vector3(self.x - other.x, self.y - other.y, self.z - other.z) def __mul__(self, scalar): return Vector3(self.x * scalar, self.y * scalar, self.z * scalar) def __truediv__(self, scalar): return Vector3(self.x / scalar, self.y / scalar, self.z / scalar) def dot(self, other): return self.x * other.x + self.y * other.y + self.z * other.z def cross(self, other): return Vector3(self.y * other.z - self.z * other.y, self.z * other.x - self.x * other.z, self.x * other.y - self.y * other.x) def magnitude(self): return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2) def normalize(self): mag = self.magnitude() if mag > 0: self.x /= mag self.y /= mag self.z /= mag # 测试代码 v1 = Vector3(1, 2, 3) v2 = Vector3(4, 5, 6) print(v1 + v2) # Vector3(5, 7, 9) print(v1 - v2) # Vector3(-3, -3, -3) print(v1 * 2) # Vector3(2, 4, 6) print(v2 / 2) # Vector3(2.0, 2.5, 3.0) print(v1.dot(v2)) # 32 print(v1.cross(v2)) # Vector3(-3, 6, -3) v3 = Vector3(3, 0, 0) v3.normalize() print(v3) # Vector3(1.0, 0.0, 0.0) ``` 这是一个简单的示例,你可以根据需要添加更多的操作符重载和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值