求从N×3矩阵提取特定数据的算法实现思路
Solution for Matrix Transformation (N×3 → 2N×N)
Let's break this down clearly, starting with your example to confirm the exact rule, then share a scalable implementation that works for any N.
Step 1: Clarify the Transformation Rule
Your original matrix is N rows × 3 columns, and the target is 2N rows × N columns. The core logic (matching your 3×3 example) is:
For each row i (0-indexed) in the original matrix:
- Create the first new row:
- In column
i, use the 2nd element (index 1) of original rowi - For every other column
j ≠ i, use the 1st element (index 0) of original rowj
- In column
- Create the second new row:
- In column
i, use the 3rd element (index 2) of original rowi - For every other column
j ≠ i, use the 1st element (index 0) of original rowj
- In column
To map this to your example:
- Original matrix:
[[1,2,3], [4,5,6], [7,8,9]] - For row 0 (
[1,2,3]):- New row 1:
[2,4,7](col0 uses row0's 2nd element, cols1/2 use row1/2's 1st elements) - New row 2:
[3,4,7](col0 uses row0's 3rd element, others same as above)
- New row 1:
- For row1 (
[4,5,6]):- New row3:
[1,5,7](col1 uses row1's 2nd element, cols0/2 use row0/2's 1st elements) - New row4:
[1,6,7](col1 uses row1's 3rd element, others same)
- New row3:
- For row2 (
[7,8,9]):- New row5:
[1,4,8](col2 uses row2's 2nd element, cols0/1 use row0/1's 1st elements) - New row6:
[1,4,9](col2 uses row2's 3rd element, others same)
- New row5:
This perfectly matches your target matrix (including the leading zero formatting we'll handle below).
Step 2: General Implementation (Python Example)
Here's a reusable function that works for any N. We'll pre-extract the first elements of each row (since we use them repeatedly) to make the code clean:
def transform_matrix(original_matrix): N = len(original_matrix) # Pre-collect the first element of each row for quick filling first_elements = [row[0] for row in original_matrix] target_matrix = [] for i in range(N): current_row = original_matrix[i] # Generate first new row (use current row's 2nd element at column i) new_row_1 = first_elements.copy() new_row_1[i] = current_row[1] # Add leading zeros to match your example formatting formatted_row_1 = [f"{num:02d}" for num in new_row_1] target_matrix.append(formatted_row_1) # Generate second new row (use current row's 3rd element at column i) new_row_2 = first_elements.copy() new_row_2[i] = current_row[2] formatted_row_2 = [f"{num:02d}" for num in new_row_2] target_matrix.append(formatted_row_2) return target_matrix # Test with your sample input sample_original = [[1,2,3], [4,5,6], [7,8,9]] result = transform_matrix(sample_original) for row in result: print(row)
Output:
['02', '04', '07'] ['03', '04', '07'] ['01', '05', '07'] ['01', '06', '07'] ['01', '04', '08'] ['01', '04', '09']
Step 3: Customization Tips
- Remove leading zeros: If you don't need the two-digit formatting, just skip the
formatted_rowlines and appendnew_row_1/new_row_2directly. - 1-indexed systems: If you're working with 1-indexed rows/columns (instead of 0-indexed), just subtract 1 from all row/column references in the code.
- Other languages: The logic translates easily to other languages (e.g., JavaScript, Java) — the core idea is to pre-collect the fill values, then iterate over each row to create two new rows with the targeted element swapped in.
内容的提问来源于stack exchange,提问作者Arthur Sávio
相关产品推荐
相关产品推荐

