এই সেকশনে আমরা দেখব কিভাবে দুটি ম্যাট্রিক্স গুণ করতে হয়। ম্যাট্রিক্স মাল্টিপ্লিকেশন শুধু তখন পারফর্ম করা যায়, যদি এটি এই শর্ত পূরণ করে। ধরা যাক দুটি ম্যাট্রিক্স A এবং B, এবং তাদের ডাইমেনশন A (m x n) এবং B (p x q) রেজাল্ট্যান্ট ম্যাট্রিক্স পাওয়া যাবে যদি এবং কেবল যদি n = p। তাহলে রেজাল্ট্যান্ট ম্যাট্রিক্স C-এর অর্ডার হবে (m x q)।
অ্যালগরিদম
matrixMultiply(A, B):
Assume dimension of A is (m x n), dimension of B is (p x q)
Begin
if n is not same as p, then exit
otherwise define C matrix as (m x q)
for i in range 0 to m - 1, do
for j in range 0 to q - 1, do
for k in range 0 to p, do
C[i, j] = C[i, j] + (A[i, k] * A[k, j])
done
done
done
End
উদাহরণ
#includeusing namespace std; int main() { int product[10][10], r1=3, c1=3, r2=3, c2=3, i, j, k; int a[3][3] = { {2, 4, 1}, {2, 3, 9}, {3, 1, 8} }; int b[3][3] = { {1, 2, 3}, {3, 6, 1}, {2, 4, 7} }; if (c1 != r2) { cout << "Column of first matrix should be equal to row of second matrix"; } else { cout << "The first matrix is:" << endl; for(i=0; i আউটপুট
The first matrix is: 2 4 1 2 3 9 3 1 8 The second matrix is: 1 2 3 3 6 1 2 4 7 Product of the two matrices is: 16 32 17 29 58 72 22 44 66