矩阵和向量乘法python3

时间:2018-06-10 14:47:48

标签: python-3.x

我无法为我的代码获取所需的输出。我遇到的问题是,当我将matrix_0vector_0相乘时,我会得到正确的输出,但对于matrix_1vector_1,它是不正确的。以及matrix_2vector_2。我是编码和python3语言的新手,并不了解原因。任何帮助都会很棒。先感谢您。我附上了我的代码链接。 code link 以下是我的代码片段

def matVec(matrix,vector):
  result = []
  for i in range(len(matrix)):
    total = 0    
    for j in range(len(vector)):
      total += matrix[i][j] * vector[j]
    result.append(total)
  return result
matrix_0 = [[1, 2],[2, 3]]
vector_0 = [8, 10]
matrix_1 = [[ 2, 2, 3]]
vector_1 = [2]
matrix_2 = [[3, 2],[4, 4],[1, 1]]
vector_2 = [1, 2, 3]
print(matVec(matrix_0,vector_0))

2 个答案:

答案 0 :(得分:0)

matrix_2和vector_2的形状未对齐。根据numpy输出测试你的代码。

<article class=main-container>
  <div class="square"></div>

  <section class="center-section-container">
    <h1 class="text1">Centro de Fisioterapia Guzmán Fernandez </h1>
    <h4 class="subtext">Fisioterapia general </h4>
      <button class="button-grey" type="button" name="button">Reservar                         
           </button>
      <button class="button-white" type="button" name="button">Pedir
           </button>
  </section>

  <aside class="aside">

    <h3 class="valoraciones"> 24 valoraciones </h3>
    <div class="number-container">
      <div class="number">8,9</div>
    </div>

    <a class="comentarios" href="#"> ver comentarios</a>

    <a class="estrella" href="#"> <img src="images/star.svg" alt="votación estrella" width="20px" height="20px" title="simbolo estrella">
    </a>


  </aside>
</article>

答案 1 :(得分:0)

您将在_1和_2中收到错误或错误答案。这个问题的原因是你的矩阵和向量不能被乘法。矩阵中的列数必须与向量中的行数相同。希望这会有所帮助,如果有需要更彻底的解释,请写评论。

你的代码非常好

相关问题