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

如何在Python中合并12个1x3矩阵为12x3矩阵?

How to Merge 12 1x3 Arrays into a 12x3 Matrix in Python

Hey there! Let's sort out this matrix merging problem for you. First, let's spot the small issue in your current setup: your comp_light function returns a list containing a 1x3 numpy array (like [array([0.44, 0.44, 0.78])]), instead of returning the array directly. That's why each loop iteration gives you a wrapped array. Here are two straightforward solutions to merge these into a 12x3 matrix:

Method 1: Modify the comp_light function to return the array directly

This is the cleanest approach—just adjust your function to return the 1x3 array instead of a list holding it. Here's the updated function:

import numpy as np
import math

def comp_light(img, mask):
    """ assume the viewer is at (0, 0, 1) """
    # Calculate centroid of gray image
    centroid = compute_centroid(img)
    # Calculate centroid of mask
    mask_centroid = compute_centroid(mask)
    mask_radius = compute_radius(mask, mask_centroid)
    r = mask_radius
    dx = centroid[1]-mask_centroid[1]
    dy = centroid[0]-mask_centroid[0]
    dy = -dy
    N = np.array([dx/r, dy/r, math.sqrt(r*r - dx*dx - dy*dy)/r])
    R = np.array([0, 0, 1])
    L = 2*np.dot(N, R)*N - R
    return L  # Return the array directly, not a list

Then, collect all the arrays in a list and convert it to a numpy matrix:

# Initialize an empty list to hold each 1x3 array
light_arrays = []

for i in range(12):
    light = comp_light(gray['chrome'][i], mask['chrome'])
    light_arrays.append(light)
    print(light)

# Convert the list of arrays into a 12x3 matrix
light_matrix = np.array(light_arrays)
print("Final matrix shape:", light_matrix.shape)  # Should output (12, 3)

Method 2: Keep your original function, extract the array from the list

If you don't want to modify your comp_light function, just extract the array from the returned list using light[0] each time:

import numpy as np

light_arrays = []

for i in range(12):
    light = comp_light(gray['chrome'][i], mask['chrome'])
    light_arrays.append(light[0])  # Grab the array inside the list
    print(light)

# Merge into a 12x3 matrix
light_matrix = np.array(light_arrays)
# Alternatively, use np.vstack for explicit vertical stacking:
# light_matrix = np.vstack(light_arrays)
print("Final matrix shape:", light_matrix.shape)

How this works

Numpy automatically converts a list of identically shaped arrays into a higher-dimensional array. Since each of your 12 elements is a 1x3 array, converting the list to a numpy array will give you a 12x3 matrix exactly what you need.

Just double-check that your compute_centroid and compute_radius functions are returning valid numerical values—this ensures each L is a properly formatted 1x3 numpy array, which is essential for the merge to work smoothly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:21:35