与向量的矩阵乘法 - C++

发布于 2024-11-18 18:44:49 字数 1080 浏览 1 评论 0原文

我正在开发一个可以乘以用户定义大小的矩阵的程序。我使用向量来存储矩阵中的值。

void Multiply(vector<float> A,vector<float> B,int rA, int cA,int rB,int cB)
{
system(CLEARSCREEN);

vector<float> C; // The resulting matrix

int sizeA=rA*cA;
int sizeB=rB*cB;
int sizeC=rA*cB;

int lrA=sizeA-1;
int lrB=sizeB-1;

int writeHead=0;

A.resize(sizeA);
B.resize(sizeB);
C.resize(sizeC);

demoDisplay(rA,rB,cA,cB,lrA,lrB,sizeA,sizeB);

for(;writeHead<=lrA; writeHead++)
{
    cout << "Please enter a value for \"" << alphabet[writeHead] << "\" in MATRIX A.\n";
    cin >> A[writeHead];
}
cout << "\n";
writeHead=0;
for (;writeHead<=lrB; writeHead++)
{
    cout << "Please enter a value for \"" << alphabet[writeHead] << "\" in MATRIX B.\n";
    cin >> B[writeHead];
}

cout << "\n\n";

displayMatrices(A,B,rA,rB,cA,cB,lrA,lrB,sizeA,sizeB);

for (int colRead=0; colRead<=cA; colRead++) {
    // somehow iterate through each element of the vector?

}   
}

我对 C++ 比较陌生,所以我不太确定如何进行两个矩阵的实际乘法。如果有人能提供帮助,那就太好了。

I am working on making a program that can multiply matrices of user-defined size. I use vectors to store the values in the matrix.

void Multiply(vector<float> A,vector<float> B,int rA, int cA,int rB,int cB)
{
system(CLEARSCREEN);

vector<float> C; // The resulting matrix

int sizeA=rA*cA;
int sizeB=rB*cB;
int sizeC=rA*cB;

int lrA=sizeA-1;
int lrB=sizeB-1;

int writeHead=0;

A.resize(sizeA);
B.resize(sizeB);
C.resize(sizeC);

demoDisplay(rA,rB,cA,cB,lrA,lrB,sizeA,sizeB);

for(;writeHead<=lrA; writeHead++)
{
    cout << "Please enter a value for \"" << alphabet[writeHead] << "\" in MATRIX A.\n";
    cin >> A[writeHead];
}
cout << "\n";
writeHead=0;
for (;writeHead<=lrB; writeHead++)
{
    cout << "Please enter a value for \"" << alphabet[writeHead] << "\" in MATRIX B.\n";
    cin >> B[writeHead];
}

cout << "\n\n";

displayMatrices(A,B,rA,rB,cA,cB,lrA,lrB,sizeA,sizeB);

for (int colRead=0; colRead<=cA; colRead++) {
    // somehow iterate through each element of the vector?

}   
}

I'm relatively new to C++, and so I'm not quite sure how to do the actual multiplication of the two matrices. If anyone could help, it would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-11-25 18:44:49

也许您被向量容器的名称误导了,这意味着一些数学用途。向量模板不提供任何矩阵乘法甚至向量乘法的函数。本例中的向量只是为您提供了一个存储矩阵的容器。显然,您以某种线性化方式存储矩阵,这将使稍后的乘法更加复杂。

请务必阅读http://www.cplusplus.com/reference/stl/vector/

此外,您并不真的想迭代向量,因为如果是这种情况,您可能只使用其他容器。您希望随机访问来手动乘以列和行。为此,您可以使用 []-operator] 或 at() 成员函数。

然后,只需手动进行乘法即可,例如此处所示(这也包括一些伪代码)。

Maybe you were mislead by the name of the vector container, that implies some mathematical use. The vector template doesn't provide any function to multiply matrices or even to multiply vectors. The vector in this case only provides you with a container to store a matrix. Obviously you store the matrices in some linearized way and that will make the multiplication more complicated later.

Be sure to read http://www.cplusplus.com/reference/stl/vector/

Furthermore you dont really want to iterate through the vectors, because if that was the case, you could have just used some other container. You want random access to do multiply the columns and rows by hand. For this you can use the []-operator] or the at() member function.

Then it is just a matter of doing the multiplication by hand, as for example shown here(which also includes some pseudo code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文