提取numpy矩阵的上三角或下三角部分 [英] Extract upper or lower triangular part of a numpy matrix

查看:1507
本文介绍了提取numpy矩阵的上三角或下三角部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵A,我想要2个矩阵UL,这样U包含A的上三角元素(所有元素都在对角线之上,但不包括对角线),并且对于L(下方的所有元素(不包括对角线).是否有numpy方法可以做到这一点?

I have a matrix A and I want 2 matrices U and L such that U contains the upper triangular elements of A (all elements above and not including diagonal) and similarly for L(all elements below and not including diagonal). Is there a numpy method to do this?

例如

A = array([[ 4.,  9., -3.],
           [ 2.,  4., -2.],
           [-2., -3.,  7.]])
U = array([[ 0.,  9., -3.],
           [ 0.,  0., -2.],
           [ 0.,  0.,  0.]])
L = array([[ 0.,  0.,  0.],
           [ 2.,  0.,  0.],
           [-2., -3.,  0.]])

推荐答案

尝试 (三角形上)和 numpy.tril (三角形-下).

Try numpy.triu (triangle-upper) and numpy.tril (triangle-lower).

这篇关于提取numpy矩阵的上三角或下三角部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆