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:

  1. The code begins by including the necessary libraries: iostream for input/output operations and vector for using dynamic arrays.
  2. The multiplyMatrices function is defined, which takes two vector<vector<int>> objects as parameters, representing the two matrices to be multiplied. It returns a vector<vector<int>> object, which represents the resulting matrix.
  3. The dimensions of the input matrices are obtained: rows1 and cols1 for the first matrix, and rows2 and cols2 for the second matrix.
  4. A vector<vector<int>> object named result is created with dimensions rows1 x cols1. This will store the result of the matrix multiplication.
  5. 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 empty result matrix is returned.
  6. 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).
  7. Inside the innermost loop, the corresponding elements of the two input matrices (mat1 and mat2) are multiplied and added to the corresponding element of the result matrix.
  8. After the multiplication is complete, the result matrix is returned.
  9. In the main function, two example matrices mat1 and mat2 are created and initialized with values.
  10. The multiplyMatrices function is called with mat1 and mat2 as arguments, and the resulting matrix is stored in the product variable.
  11. The resulting matrix product is then printed element by element using nested loops.
  12. 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.