目录
平均分4份
import numpy as np
# 创建一个示例矩阵
matrix = np.array([[1, 2, 3, 4,4],
[5, 6, 7, 8,8],
[9, 10, 11, 12,12],
[13, 14, 15, 16,17]])
# 将矩阵均分为4份
num_rows, num_cols = matrix.shape
split_rows = num_rows // 2
split_cols = num_cols // 2
quarters = [
matrix[:split_rows, :split_cols], # 左上角1/4
matrix[:split_rows, split_cols:], # 右上角1/4
matrix[split_rows:, :split_cols], # 左下角1/4
matrix[split_rows:, split_cols:], # 右下角1/4
]
# 从中间再取1/4
middle_quarter = matrix[split_rows//2:3*split_rows//2, split_cols//2:3*split_cols//2]
# 打印结果
print("初始矩阵:")
print(matrix)
print("\n均分为4份:")
for i, quarter in enumerate(quarters):
print(f"Quarter {i + 1}:\n{quarter}\n")
print("从中间再取1/4:")
print(middle_quarter)
切为16份
import numpy as np
# 假设 arr 是你的原始数组
arr = np.random.rand(32, 24) # 这里使用了一个随机生成的数组作为示例
# 将数组切割为16份
num_splits = 16
splits = np.array_split(arr, num_splits, axis=0) # 在axis=0方向上切割,即按行切割
# 打印每个块的形状
for i, split in enumerate(splits):
print(f"Split {i + 1}: {split.shape}")