Matrix addition
Two matrices can be added only if they have the same dimensions. The result will be a matrix of the same dimensions. To perform the addition, numbers in matching postions in the input matrices are added and the result is placed in the same position in the output matrix. Java Codes
Example: Adding 2x2 Matrices
Let us add 2 matrices of dimension 2x2, let them be and .
These matrices can be added, because they are both 2x2. The result will also be 2x2. The result is:
Addition is commutative in general for matrices, i.e. .
More General Approach
Matrix addition can be performed on matrices of any dimensions, as long as they both have the same dimensions.
Let us visualize A and B as m×n matrices.
We are going to be adding like before, but generally, sot the result is:
General Algorithm
Here's a general algorithm for adding matrices:
- DONT Check the sizes of two matrices (m×n) and (t×u): if m = t and n = u then we can add them o
- therwise we just can't do it.
- If they can be added, then create a new square matrix of size m×n.
- For each element in A, find the element at the same position in B (i.e. same row and column) and add the 2 values. Place the result of this addition into the result matrix in the same position again.
Pseudocode
The following pseudocode adds matrices of size m×n.<img data-rte-meta="%7B%22type%22%3A%22ext%22%2C%22placeholder%22%3A1%2C%22wikitext%22%3A%22%3Cpre%3E%5Cn%5Cn%5Cn%3C%5C%2Fpre%3E%22%7D" data-rte-instance="3585-18826377344f23bd3ae30ef" class="placeholder placeholder-ext" src="data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D" type="ext" />Algorithm addMatrix (matrix1, matrix2, size, matrix3)
Add matrix1 to matrix2 and result in matrix3
Pre matrix1 and matrix2 have data
size is number on colums or rows in matrix
Post matrices added --- result in matrix31 loop (not end of row)1 loop (not end of colum)1 add matrix1 and matrix2 cells2 store sum in matrix32 end loop2 end loopend addMatrixReference: www.noormustafa.com
Implementations
PHP
function matrixAddition($m1,$m2){
$n = count($m1) - 1;
for($i=0;$i<=$n;$i++){
for($j=0;$j<=$n;$j++){
$a[$i][$j] = $m1[$i][$j]+$m2[$i][$j];
}
}
return $a;
}
C Sharp
This article is missing a code example in the C Sharp language.
Visual Basic
Public Function AddMatrices(
matrixA As IEnumerable(Of IEnumerable(Of Double)),
matrixB As IEnumerable(Of IEnumerable(Of Double))) _
As IEnumerable(Of IEnumerable(Of Double))
Dim result As New List(Of ReadOnlyCollection(Of Double))
Dim aRows = matrixA.GetEnumerator
Dim bRows = matrixB.GetEnumerator
Dim a = aRows.MoveNext
Dim b = bRows.MoveNext
Do While a Or b
If a <> b Then Throw New ArgumentException
Dim aCols = aRows.Current.GetEnumerator
Dim bCols = bRows.Current.GetEnumerator
Dim resultRow As New List(Of Integer)
Dim a2 = aCols.MoveNext
Dim b2 = bCols.MoveNext
Do While a2 Or b2
If a2 <> b2 Then Throw New ArgumentException
resultRow.Add(aCols.Current + bCols.Current)
a2 = aCols.MoveNext
b2 = bCols.MoveNext
Loop
result.Add(New ReadOnlyCollection(Of Double)(resultRow))
a = aRows.MoveNext
b = bRows.MoveNext
Loop
Return New ReadOnlyCollection(Of ReadOnlyCollection(Of Double))(result)
End Function
Python
This article is missing a code example in the Python language.
Ruby
This article is missing a code example in the Ruby language.
JavaScript
This article is missing a code example in the JavaScript language.
Java
This article is missing a code example in the Java language.
import java.util.Scanner;
public class AdditionMatrix
{
public static void main(String[] args)
{
int m, n, i, j;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows of matrix");
m = input.nextInt();
System.out.println("Enter the number of columns of matrix");
n = input.nextInt();
int matrixA[][] = new int[m][n];
int matrixB[][] = new int[m][n];
int sumOfMatrices[][] = new int[m][n];
System.out.println("Enter the elements of Matrix A");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
matrixA[i][j] = input.nextInt();
System.out.println("Enter the elements of Matrix B");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
matrixB[i][j] = input.nextInt();
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
sumOfMatrices[i][j] = matrixA[i][j] + matrixB[i][j];
System.out.println("Sum of entered matripes:-");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
System.out.print(sumOfMatrices[i][j] + "\t");
System.out.println();
}
}
}
Squeak
This article is missing a code example in the Squeak language.
Smalltalk
This article is missing a code example in the Smalltalk language.
Scheme
This article is missing a code example in the Scheme language.
Lisp
(defun array-sum (m1 m2)
"Returns the element-wise sum of two arrays of the same dimensions."
(if (equal (array-dimensions m1)
(array-dimensions m2))
(let ((sum (make-array (array-dimensions m1))))
(dotimes (i (array-total-size m1))
(setf (row-major-aref sum i)
(+ (row-major-aref m1 i)
(row-major-aref m2 i))))
sum)
(error "Arrays' dimensions are different.")))
C
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int rows;
int columns;
int *data;
} t_matrix;
#define NEW_MATRIX(m,r,c) (m)=malloc(sizeof(t_matrix)); (m)->rows=(r);(m)->columns=(c);(m)->data=malloc(sizeof(int)*(r)*(c));
#define VALUE_at(m, r, c) (m)->data[c*(m)->rows + r]
#define FREE(x) { free(x); (x)= NULL; }
#define FREE_MATRIX(m) FREE((m)->data);FREE(m)
t_matrix* addMatrix(const t_matrix *A, const t_matrix *B)
{
t_matrix *C;
int r,c;
NEW_MATRIX(C, A->rows, A->columns);
for(r=0; r<A->rows; r++)
{
for(c=0; c<A->columns; c++)
{
VALUE_at(C, r, c) = VALUE_at(A, r, c) + VALUE_at(B, r, c);
}
}
return C;
}
void main(void)
{
t_matrix *A, *B, *C;
int r,c;
NEW_MATRIX(A, 4,4);
NEW_MATRIX(B, 4,4);
for(r=0; r<A->rows; r++)
for(c=0; c<A->columns; c++)
{
VALUE_at(A, r, c) = 1;
VALUE_at(B, r, c) = 2;
}
C = addMatrix(A, B);
for(r=0; r<C->rows; r++)
{
for(c=0; c<C->columns; c++)
{
printf("%d ", VALUE_at(C, r, c) );
}
printf("\n");
}
FREE_MATRIX(A);
FREE_MATRIX(B);
FREE_MATRIX(C);
}
