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

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

\[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
相关推荐
段一凡-华北理工大学2 小时前
高炉智能布料技术与炉料分布优化~系列文章03:布料矩阵解码:环位、角度、圈数与料流的量化控制
java·网络·人工智能·矩阵·高炉智能化·高炉布料矩阵优化·高炉智能布料技术
智购无人售货机厂家18 小时前
2026自动售货机软硬件版本管理策略:从版本号规范到兼容性矩阵的工程实践~YH
运维·服务器·人工智能·单片机·嵌入式硬件·线性代数·矩阵
龙泽金1 天前
矩阵论简介与工程应用
数学建模·矩阵·自动控制
独立开发之道2 天前
【three.js教程】Three.js 运行时更新:矩阵、几何体、材质怎么改才不卡
javascript·矩阵·材质
张宇Joaquin2 天前
鲲鹏统一并行加速库KUPL--异步数据拷贝
线性代数·算法·矩阵·numpy
独立开发之道2 天前
【three.js教程】Three.js 矩阵变换:position/quaternion/scale 和 matrix 到底怎么配合
开发语言·javascript·矩阵
小翰生信2 天前
转录组下游分析全流程:DESeq2 差异分析、GO/KEGG 富集与 GSEA 实战
数据库·矩阵·golang
AI_yangxi3 天前
靠谱的短视频矩阵系统
大数据·人工智能·矩阵
浪里镖客3 天前
位姿转换矩阵写法-个人习惯(计算机理解其实是相反的)
线性代数·算法·矩阵
张宇Joaquin3 天前
鲲鹏计算平台矩阵乘算子SGEMM优化实践——下篇
线性代数·矩阵