【问题标题】:How can I multiply a vector with a matrix without numpy in Pythonpython - 如何在Python中将向量与没有numpy的矩阵相乘
【发布时间】:2022-11-14 00:45:07
【问题描述】:

我正在尝试将矩阵与向量相乘,但我找不到不使用 NumPy 来制作向量的方法 我需要找到一种方法来创建一个没有 numpy 的向量,这样我就可以将它与矩阵相乘

我尝试了一个我在这里找到的答案,但是当我尝试使用它时它似乎不起作用。当我运行它时它没有做任何事情没有错误没有响应没有 我只是运行它,没有任何反应

这是我从这里的答案中找到的代码

def multiply(v, G):
result = []
for i in range(len(G[0])): #this loops through columns of the matrix
    total = 0
    for j in range(len(v)): #this loops through vector coordinates & rows of matrix
        total += v[j] * G[j][i]
    result.append(total)
return result

所有这些都在 jupyter notebook 中编码

【问题讨论】:

  • 如果您不使用 NumPy,您将值包含在什么结构中?
  • 欢迎来到 SO!到目前为止,您到底尝试了什么?我们在这里更多地帮助解决“我尝试了 X,但它没有达到我的预期,反而导致了错误!”形式的特定问题。伴随着Minimal, Complete, and Verifiable example
  • 列表对象可以用作向量。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。
  • 我不知道阅读这篇文章的实际问题是什么

标签: python vector multiplication


【解决方案1】:

首先你的代码没有正确缩进下面提供了正确的缩进

def multiply(v, G):
    result = []
    for i in range(len(G[0])): #this loops through columns of the matrix
        total = 0
        for j in range(len(v)): #this loops through vector coordinates & rows of matrix
            total += v[j] * G[j][i]
        result.append(total)
    return result

然后,您需要实际调用乘法函数并使用打印函数在您的 jupyter 笔记本中显示您想要的输入的结果。为此,请在您的 jupyter 笔记本单元格中运行以下代码。

v=[1,2,3]# input vector
G=[[1,2,3],[4,5,6],[7,8,9]] #input matrix
print(multiply(v,G))# this will print your result

【讨论】:

    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多