在指定条件下获取布尔矩阵中的索引矩阵

问题重新描述如下,设计程序

\[False False False True True True

True False True False False True

False False False True True True

True False False False False False

False False False False False False

True False False False False False

True True False True False False

False False False True False False

True False True False False True

False False False True True True

True True False True False False

True False True False False True

True True False True False False\]

使用 np.where查找前面布尔矩阵中,

1、如果1,4,6列有两个True,则给我返回1,4,6列值为True的行列索引,放在前两个位置(行对索引应一个矩阵,列索引对应一个矩阵),剩下的那一个没有True的则返回该元素原始索引,放在第三个位置。

2、2,3,5列中最多有一个True,返回它的行列索引,放在返回数组的第四个位置。如果1,4,6列中True的个数小于2,就返回 1,4,6列的原始行列索引(放在返回数组的1,2,3列),以及第二列的索引(放在返回数组的第4列)。

3、最终返回两个索引数组,一个是行索引数组,另一个是列索引数组,每一行的索引必须与布尔矩阵中的行相对应

python 复制代码
def elegant_solution(arr):
    """
    修正的向量化版本,无循环
    """
    n_rows = arr.shape[0]
    
    # 1. 行索引数组
    row_array = np.repeat(np.arange(n_rows)[:, None], 4, axis=1)
    
    # 2. 预定义列
    cols_146 = np.array([0, 3, 5])
    cols_235 = np.array([1, 2, 4])
    
    # 3. 初始化列索引数组
    col_array = np.zeros((n_rows, 4), dtype=int)
    
    # 4. 计算第1,4,6列True数量
    counts_146 = arr[:, cols_146].sum(axis=1)
    
    # 5. 区分两种情况
    mask_eq2 = counts_146 == 2
    mask_lt2 = counts_146 < 2
    
    # 6. 处理mask_lt2为True的情况(True个数小于2)
    # 设置默认值:第1,4,6列和第2列
    col_array[mask_lt2] = np.array([0, 3, 5, 1])
    
    # 7. 处理mask_eq2为True的情况(True个数等于2)
    if np.any(mask_eq2):
        rows_eq2 = np.where(mask_eq2)[0]
        n_eq2 = len(rows_eq2)
        
        if n_eq2 > 0:
            # 提取这些行的第1,4,6列
            arr_146_sub = arr[rows_eq2][:, cols_146]
            
            # 方法:使用argsort分离True和False
            # 对每行进行排序,True(1)会排在False(0)后面
            sorted_indices = np.argsort(arr_146_sub, axis=1)  # 默认升序,False在前
            
            # 提取True的位置(最后两个)和False的位置(第一个)
            true_indices = sorted_indices[:, -2:]  # 形状: (n_eq2, 2)
            false_indices = sorted_indices[:, 0:1]  # 形状: (n_eq2, 1)
            
            # 转换为原始列索引
            true_cols = cols_146[true_indices]
            false_cols = cols_146[false_indices]
            
            # 填充前三个位置
            col_array[rows_eq2, 0] = true_cols[:, 0]
            col_array[rows_eq2, 1] = true_cols[:, 1]
            col_array[rows_eq2, 2] = false_cols[:, 0]
            
            # 处理第2,3,5列
            arr_235_sub = arr[rows_eq2][:, cols_235]
            
            # 找到每行第一个True的位置
            # 使用cumsum找到第一个True
            cumsum_arr = np.cumsum(arr_235_sub, axis=1)
            first_true_mask = (cumsum_arr == 1) & arr_235_sub
            
            # 获取列索引
            # argmax会返回第一个最大值的位置,我们确保每行至多一个True
            first_true_idx = np.argmax(first_true_mask, axis=1)
            
            # 检查是否找到True
            has_true = np.any(first_true_mask, axis=1)
            
            # 转换并填充
            # 注意:当没有True时,argmax返回0,但我们需要-1
            # 所以使用where根据has_true选择值
            col_235_vals = np.where(has_true, cols_235[first_true_idx], -1)
            col_array[rows_eq2, 3] = col_235_vals
    
    return row_array, col_array
相关推荐
微三云 - 廖会灵 (私域系统开发)3 天前
企业大模型应用工程化思考:跳出 Demo 陷阱,构建「业务‑资产‑Agent」三层落地架构
人工智能·矩阵·重构
tryxr3 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
自传.4 天前
B 站更新|软考架构师案例篇・数据库篇第 1-2 集上线|数据库规范化|索引视图物化视图真题精讲
数据库·软考高级·系统架构师·索引·案例分析·2026软考·软考系统架构师
俗人六哥AI企业获客盈利系统5 天前
知识库才是AI落地的分水岭
人工智能·线性代数·矩阵·自动化
微三云 - 廖会灵 (私域系统开发)6 天前
私域电商多端商城系统架构实践:小程序 / APP / H5 一套后端如何落地
矩阵·重构·零售
微三云 - 廖会灵 (私域系统开发)6 天前
多端商城统一账号体系设计实践:小程序 / APP / H5 登录打通与账号合并
矩阵·重构·自动化·零售
智购无人售货机厂家9 天前
2026自动售货机品类拓展逻辑:从饮料零食到鲜食药品文创的工程实践~YH
服务器·人工智能·单片机·线性代数·矩阵
IT小白杨10 天前
社媒多账号运营的环境隔离:一张图谱模型,解释清“为什么换IP还是被连坐“
经验分享·笔记·矩阵·指纹浏览器
镜像视界(浙江)科技有限公司11 天前
《视频孪生之上:二维展示终结,三维空间计算重构城市逻辑》——跨摄像连续表达 × 三角测量厘米级定位 × 动态轨迹建模,构建新一代城市空间
大数据·人工智能·算法·矩阵·音视频·空间计算
wzdark11 天前
稀疏矩阵存储结构及其运算复杂度研究4
线性代数·矩阵