题解|将向量转换为对角矩阵
将向量转换为对角矩阵
https://www.nowcoder.com/practice/fa80e7e3e07345b1993b93f46182643b?tpId=377&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj
将向量转换为对角矩阵(Diagonal Matrix)是一种常用的矩阵,其计算公式为:
其中,是向量。
通俗的说,就是将向量中的每个元素作为对角矩阵的对角线上的元素。
标准代码如下
def make_diagonal(x):
identity_matrix = np.identity(np.size(x))
return (identity_matrix*x)