python - numpy合并上三角和下三角

标签 python numpy matrix

我基本上想做与 this question 相反的事情.我有两个用 np.trilnp.triu 拆分的矩阵,我想将它们重新组合成一个矩阵。

A = array([[ 0. ,  0. ,  0. ],
           [ 0.1,  0. ,  0. ],
           [ 0.6,  0.5,  0. ]])

B = array([[ 0. ,  0.4,  0.8],
           [ 0. ,  0. ,  0.3],
           [ 0. ,  0. ,  0. ]])

我希望它看起来像

array([[ 0. ,  0.4,  0.8],
       [ 0.1,  0. ,  0.3],
       [ 0.6,  0.5,  0. ]])

是否有内置的 numpy 函数来执行此操作?

最佳答案

你是说 A+B

import numpy
A = numpy.array([[ 0. ,  0. ,  0. ],
           [ 0.1,  0. ,  0. ],
           [ 0.6,  0.5,  0. ]])

B = numpy.array([[ 0. ,  0.4,  0.8],
           [ 0. ,  0. ,  0.3],
           [ 0. ,  0. ,  0. ]])

print A+B

返回

array([[ 0. ,  0.4,  0.8],
       [ 0.1,  0. ,  0.3],
       [ 0.6,  0.5,  0. ]])

关于python - numpy合并上三角和下三角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737757/

相关文章:

python - 当为具有多个输出的模型尝试 train_on_batch 时,Keras 中的 sample_weight 出现问题

python - 一个numpy数组的 View 是一个 View 的副本?

c - C 中动态分配的字符串矩阵

python - 我应该向我的 CNN 提供什么?大输入矩阵还是 10,000 个小输入矩阵?

python - pytest.mark.parametrize 中的 indirect = True/False 是什么意思?

python - 将多个列表水平组合成一个列表

python - tensorflow 中 nd 数组输入的占位符定义

python - 矩阵乘以 numpy 矩阵数组

python - Anaconda 4.0.2 是否已经在 MKL 上运行 numpy

c++ - 如何实现 Matlab 的 mldivide(又名反斜杠运算符 "\")