【发布时间】:2021-08-30 10:38:04
【问题描述】:
我需要一个将矩阵和向量相乘的函数 (Matrix*vector)
它接受一个矩阵 A 和一个向量 B,用 int 描述维度。不知何故,它运行不正确。有什么帮助吗??
void Multiply(double *res, double **A, double *B, int ARows, int ACols, int BRows)
{
if (ACols !=BRows)
{
return;
}
for (int i = 0; i < ACols; i++)
{
res[i] = 0;
for (int j = 0; j < BRows; j++)
{
res[i] += A[i][j]*B[j];
}
}
}
【问题讨论】:
-
不应该在某处使用
ARows吗? -
首先应该是 Arows,而不是 Acols。注意res的行数和A一样,分配正确了吗?
-
也许
std::valarray可以提供帮助。
标签: c++ loops for-loop nested-loops function-definition