You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何简便实现特定NumPy数组的重塑与拼接转换?

Efficient Vectorized Solution for Your Matrix Reshaping & Concatenation Task

First, let's confirm the core transformation you need: taking your 3D input array, reshaping each inner 1D subarray into a 2x2 matrix, concatenating pairs of these matrices horizontally, then stacking all those concatenated results vertically.

The good news is you can do this without loops using NumPy's vectorized operations—perfect for scaling to larger matrices. Here's a clean, scalable approach:

Step-by-Step Explanation

Let's start with your example input:

import numpy as np

# Your input 3D array
arr = np.array([[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]])

1. Reshape to split inner 1D arrays into 2x2 matrices

First, we reshape the input to explicitly separate the 2x2 structure of each inner subarray. The input shape is (2, 2, 4); we'll reshape it to (2, 2, 2, 2):

reshaped = arr.reshape(-1, 2, 2, 2)

The -1 lets NumPy automatically calculate the first dimension (which is 2 here, matching the number of outer groups). Now the structure is (number_of_groups, matrices_per_group, rows_per_matrix, cols_per_matrix).

2. Transpose to align rows for horizontal concatenation

Next, we swap axes to align the rows from paired matrices. This lets us merge them horizontally in the next step:

transposed = reshaped.transpose(0, 2, 1, 3)

Now the shape is still (2, 2, 2, 2), but the structure shifts to (number_of_groups, rows_per_matrix, matrices_per_group, cols_per_matrix)—so each row now contains the corresponding row from both matrices in the group.

3. Final reshape to merge columns

Finally, we flatten the last two dimensions to get the horizontal concatenation we want, and stack all results vertically:

result = transposed.reshape(-1, 4)

You can also condense this into a single line for brevity:

result = arr.reshape(-1, 2, 2, 2).transpose(0, 2, 1, 3).reshape(-1, 4)

Verify the Result

Running this code gives exactly the output you need:

print(result)
# Output:
# [[ 1  2  5  6]
#  [ 3  4  7  8]
#  [ 9 10 13 14]
#  [11 12 15 16]]

Scaling to Larger Matrices

This approach works for any size where:

  • Your input is a 3D array with shape (G, M, N*K) (G = number of groups, M = matrices per group, N*K = length of each inner 1D array, which will be reshaped to NxK matrices)
  • You want to concatenate the M matrices per group horizontally (along columns), then stack all groups vertically.

For example, if you had inner arrays of length 6 (to reshape to 2x3 matrices), adjust the code to:

# For 2x3 matrices per inner array
result = arr.reshape(-1, M, 2, 3).transpose(0, 2, 1, 3).reshape(-1, M*3)

Just replace 2,3 with your target matrix dimensions, and M*3 with M * cols_per_matrix.

内容的提问来源于stack exchange,提问作者Netchaiev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:08:12