Algorithm For Matrix Addition-1
Algorithm For Matrix Addition-1
If A and B are two matrices of same order m x n (to be read as m by n matrix) then their
addition is defined by A + B. For example if A is a matrix of order 2×3 and B is a matrix
of same order 2×3 then addition is possible and the resultant matrix will be of order
2×3. If a matrix A has its order 2×3 and another matrix B has its order 3×2 then addition
is not possible. So before adding any number of matrices we must check that whether
each of the matrices are of same order or not then we need to proceed further.
ALGORITHM:
(Matrix addition Algorithm) Suppose A and B are two matrix arrays of order m x n,
and C is another matrix array to store the addition result. i, j are counters.
Step1: Start
Step2: Read: m and n
Step3: Read: Take inputs for Matrix A[1:m, 1:n] and Matrix B[1:m, 1:n]
Step4: Repeat for i := 1 to m by 1:
Repeat for j := 1 to n by 1:
C[i, j] := A[i, j] + B[i, j]
[End of inner for loop]
[End of outer for loop]
Step5: Print: Matrix C
Step6: Exit.
Also See:
ALGORITHM:
(Matrix Subtraction Algorithm) Suppose A and B are two matrix arrays of order m x n,
and C is another matrix array to store the addition result. i, j are counters.
Step1: Start
Step2: Read: m and n
Step3: Read: Take inputs for Matrix A[1:m, 1:n] and Matrix B[1:m, 1:n]
Step4: Repeat for i := 1 to m by 1:
Repeat for j := 1 to n by 1:
C[i, j] := A[i, j] – B[i, j]
[End of inner for loop]
[End of outer for loop]
Step5: Print: Matrix C[1:m, 1:n]
Step6: Exit.
(Matrix Multiplication Algorithm) Suppose A and B are two matrices and their order
are respectively m x n and p x q. i, j and k are counters. And C to store result.
Step1: Start.
Step2: Read: m, n, p and q
Step3: Read: Inputs for Matrices A[1:m, 1:n] and B[1:p, 1:q].
Step4: If n ≠ p then:
Print: Multiplication is not possible.
Else:
Repeat for i := 1 to m by 1:
Repeat for j := 1 to q by 1:
C[i, j] := 0 [Initializing]
Repeat k := 1 to n by 1
C[i, j] := C[i, j] + A[i, k] x B[k, j]
[End of for loop]
[End of for loop]
[End of for loop]
[End of If structure]
Step5: Print: C[1:m, 1:q]
Step6: Exit.