[英]2D OR Multidimensions Matrix Multiplication using a Vector In C++
I need to declare a vector which will contain 3 x 3 matrix.我需要声明一个包含 3 x 3 矩阵的向量。 And Need to pass that vector to a function and need to perform a multiplication with the same vector.
并且需要将该向量传递给 function 并且需要使用相同的向量执行乘法运算。 I know, I am not going in a proper way.
我知道,我走的路不对。 But I am not able to figure out the problem.
但我无法弄清楚问题所在。 I am hoping some direction about 2d Matrix vector Multiplication.
我希望有关 2d 矩阵向量乘法的一些方向。
#include <iostream>
#include <vector>
using namespace std;
void display(vector<vector<int>> &vec)
{
for (int i = 0; vec.size(); i++)
{
for (int j = 0; j < vec.size(); j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
}
vector<vector<int>> matMultiply(vector<vector<int>> &vec)
{
for (int i = 0; vec.size(); i++)
{
for (int j = 0; j < vec.size(); j++)
vec[i][j] = 0;
{
for (int k = 0; k < vec.size(); i++)
{
vec[i][j] *= vec[i][k] * vec[k][j];
cout << vec[i][j] << " ";
}
}
cout << endl;
return vec;
}
}
int main()
{
vector<vector<int>> matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
display(matrix);
matMultiply(matrix);
return 0;
}
Because matrix multiplication requires multiple matrix values to compute the value at a single index, you need some way to store the intermediate result.因为矩阵乘法需要多个矩阵值来计算单个索引处的值,所以您需要某种方式来存储中间结果。 probably in another
vector
.可能在另一个
vector
中。 You need to create a new std::vector<std::vector<int>>
inside the matMultiply
function.您需要在
matMultiply
function 中创建一个新的std::vector<std::vector<int>>
。 It should like something like this:它应该像这样:
std::vector<std::vector<int>> matMultiply(std::vector<std::vector<int>> &vec){
std::vector<std::vector<int>> result(3,vector<int>(3));
for(int i = 0; result.size(); i++){
for (int j = 0; j < result.size(); j++) {
result[i][j] = 0;
for (int k = 0; k < result.size(); i++){
result[i][j] *= vec[i][k] * vec[k][j];
std::cout << result[i][j] << " ";
}
}
std::cout << std::endl;
return result;
}
}
Note: For future questions, please indent your code properly before posting here.注意:对于以后的问题,请在此处发布之前正确缩进您的代码。
@Taimoor Zaeem. @Taimoor Zaeem。 Thank you again.
再次感谢你。 I did the code as you suggested.
我按照你的建议做了代码。 But the result is coming wrong.
但结果是错误的。 Beside the display function is not working, may be I did wrong.. For the viewing the result, I have add
std:: cout<< mat[i][j]<<" ";
在显示器旁边 function 不工作,可能是我做错了.. 为了查看结果,我添加了
std:: cout<< mat[i][j]<<" ";
in the matMultiply
function The result is coming在
matMultiply
结果来了
1 9 30
2 12 36
3 15 42
4 24 66
8 33 81
12 42 96
7 39 102
14 54 126
21 69 150
The result will be结果将是
30 36 42
66 81 96
102 126 150
#include<iostream>
#include<vector>
void display (std::vector<std::vector<int>> mat){
for(int i = 0; mat.size(); i++){
for (int j = 0; j < mat.size(); j++) {
std :: cout<< mat[i][j]<<" ";
}
std :: cout<< std ::endl;
}
}
std::vector<std::vector<int>> matMultiply(const std::vector<std::vector<int>> &vec){
std::vector<std::vector<int>> result(3, std::vector<int>(3));
for(int i = 0; result.size(); i++){
for (int j = 0; j < result.size(); j++) {
for (int k = 0; k < result.size(); k++){
result[i][j] += vec[i][k] * vec[k][j];
std :: cout<< result[i][j] <<" ";
}
std :: cout<< std:: endl;
}
}
return result;
}
int main(){
std :: vector < std :: vector< int > > matrix = {{1,2,3},{4,5,6},{7,8,9}};
//display(matrix);
matMultiply(matrix);
display(matMultiply(matrix));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.