Python - 将具有不同列数的矩阵相乘

标签 python numpy matrix matrix-multiplication

我正在尝试将两个矩阵 A 和 B 相乘,其中 B 的列数比 A 多,最好使用 python 和 numpy。

例子:

A = numpy.matrix([[2,3,15],[5,8,12],[1,13,4]], dtype=numpy.object)
B = numpy.matrix([[2,15,6,15,8,14],[17,19,17,7,18,14],[24,14,0,24,2,11]], dtype=numpy.object)

( A*B ) = [[415,297,63,411,100,235],[434,395,166,419,208,314], [319,318,227,202,250,240]]

我找到了一些例子,如果它们是数组,但如果它们是矩阵,则没有。有人可以帮帮我吗?

最佳答案

你真的试过这个吗?它在这里工作正常:

import numpy as np

def main():
    A = np.matrix([[2,3,15],[5,8,12],[1,13,4]], dtype=np.object)
    B = np.matrix([[2,15,6,15,8,14],[17,19,17,7,18,14],[24,14,0,24,2,11]], dtype=np.object)

    C = ( A*B ) % 26 #  = [[25,11,11,21,22,1],[18,5,10,3,0,2], [7,6,19,20,16,6]]
    print(C)
    return 0

if __name__ == '__main__':
    main()

打印:

[[25 11 11 21 22 1]
 [18 5 10 3 0 2]
 [7 6 19 20 16 6]]

关于Python - 将具有不同列数的矩阵相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429608/

相关文章:

python - 绘制 ROC 曲线 - 索引错误太多

c++ - 快速访问二维矩阵中的单元格

python - 在 Python 中比较矩阵并存储结果

python - 为什么 scikit 学习分词器不能正确分词 utf-8(葡萄牙语)文本?

python - 使用 Python 和 Beautiful Soup 从 .html 文件中提取文本,删除 HTML,然后写入文本文件

python - numpy 数组迭代以跟踪到目前为止的最大数量

python - Numpy-native "nested-for-like"结构

matlab - 在 MATLAB 中插入矩阵作为矩阵的元素

python - 访问创建另一个类的类

python - Matplotlib - 使用 latex 渲染文本时无法将图形保存为 eps 文件