multiply two Mat in c++ element per element
#include <iostream>
#include <vector>
std::vector<std::vector<int>> multiplyMatrices(const std::vector<std::vector<int>>& mat1, const std::vector<std::vector<int>>& mat2) {
int rows1 = mat1.size();
int cols1 = mat1[0].size();
int rows2 = mat2.size();
int cols2 = mat2[0].size();
std::vector<std::vector<int>> result(rows1, std::vector<int>(cols1));
if (cols1 != rows2) {
std::cout << "Invalid matrices dimensions. Cannot multiply." << std::endl;
return result;
}
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
return result;
}
int main() {
std::vector<std::vector<int>> mat1 = {{1, 2}, {3, 4}};
std::vector<std::vector<int>> mat2 = {{5, 6}, {7, 8}};
std::vector<std::vector<int>> product = multiplyMatrices(mat1, mat2);
std::cout << "Product of matrices:" << std::endl;
for (const auto& row : product) {
for (const auto& element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
return 0;
}
Explanation:
- The code begins by including the necessary libraries:
iostream
for input/output operations andvector
for using dynamic arrays. - The
multiplyMatrices
function is defined, which takes twovector<vector<int>>
objects as parameters, representing the two matrices to be multiplied. It returns avector<vector<int>>
object, which represents the resulting matrix. - The dimensions of the input matrices are obtained:
rows1
andcols1
for the first matrix, androws2
andcols2
for the second matrix. - A
vector<vector<int>>
object namedresult
is created with dimensionsrows1
xcols1
. This will store the result of the matrix multiplication. - A check is performed to ensure that the number of columns in the first matrix (
cols1
) is equal to the number of rows in the second matrix (rows2
). If they are not equal, the matrices cannot be multiplied, so an error message is displayed and an emptyresult
matrix is returned. - Nested loops are used to iterate over each element of the resulting matrix
result
. The outer loop iterates over the rows of the first matrix, the middle loop iterates over the columns of the second matrix, and the inner loop iterates over the columns of the first matrix (or the rows of the second matrix, they are equal in this case). - Inside the innermost loop, the corresponding elements of the two input matrices (
mat1
andmat2
) are multiplied and added to the corresponding element of theresult
matrix. - After the multiplication is complete, the
result
matrix is returned. - In the
main
function, two example matricesmat1
andmat2
are created and initialized with values. - The
multiplyMatrices
function is called withmat1
andmat2
as arguments, and the resulting matrix is stored in theproduct
variable. - The resulting matrix
product
is then printed element by element using nested loops. - The program ends with a return statement.
Note: This code assumes that the input matrices have compatible dimensions for multiplication, and the resulting matrix will have dimensions rows1
x cols2
. If the matrices have incompatible dimensions, the code will display an error message and return an empty matrix.