本文介绍了在Numpy中制作特殊的对角矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个看起来像这样的 numpy 数组:
I am trying to make a numpy array that looks like this:
[a b c ]
[ a b c ]
[ a b c ]
[ a b c ]
所以这涉及更新主对角线及其上方的两条对角线.
So this involves updating the main diagonal and the two diagonals above it.
这样做的有效方法是什么?
What would be an efficient way of doing this?
推荐答案
这是一个 Toeplitz 矩阵的例子 - 您可以使用 scipy.linalg.toeplitz
:
This is an example of a Toeplitz matrix - you can construct it using scipy.linalg.toeplitz
:
import numpy as np
from scipy.linalg import toeplitz
first_row = np.array([1, 2, 3, 0, 0, 0])
first_col = np.array([1, 0, 0, 0])
print(toeplitz(first_col, first_row))
# [[1 2 3 0 0 0]
# [0 1 2 3 0 0]
# [0 0 1 2 3 0]
# [0 0 0 1 2 3]]
这篇关于在Numpy中制作特殊的对角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!