简体   繁体   English

如何将nxn矩阵转换为1x1矩阵(python)

[英]How to convert nxn matrix into 1x1 matrix (python)

Adding values in the rows and columns of a nxn matrix how can I form a 1x1 matrix?在 nxn 矩阵的行和列中添加值如何形成 1x1 矩阵?

A scalar can be viewed as a 1x1 matrix.标量可以看作是一个 1x1 矩阵。

If your matrix is a 2d - numpy array -> np.sum(mat) where mat is your matrix.如果您的矩阵是 2d - numpy 数组 - > np.sum(mat)其中mat是您的矩阵。

If your matrix is a list of lists -> sum([sum(x) for x in mat]) where mat is your matrix.如果您的矩阵是列表列表 -> sum([sum(x) for x in mat])其中mat是您的矩阵。

If you need sum per columns/rows I suggest you use numpy, with np.sum(mat, axis=AXIS) where AXIS tells whether you want sum for rows or columns.如果您需要每列/行的总和,我建议您使用 numpy 和np.sum(mat, axis=AXIS) ,其中 AXIS 告诉您是否需要行或列的总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM