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 Edit

Let us add 2 matrices of dimension 2x2, let them be \mathbf{A}and \mathbf{B}.

 \mathbf{A} = \begin{bmatrix}
     1 & 2   \\ 
     3 & 4   
  \mathbf{B} = \begin{bmatrix}
     5 & 6    \\ 
     -7 & -2

These matrices can be added, because they are both 2x2. The result will also be 2x2. The result is:

\mathbf{A+B} = \begin{bmatrix}
     1 + 5 & 2 + 6  \\ 
     3 + -7 & 4 + -2 
  = \begin{bmatrix}
     6 & 8  \\ 
     -4 & 2  

Addition is commutative in general for matrices, i.e. \mathbf{A+B} = \mathbf{B+A}.

More General ApproachEdit

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.

  \mathbf{A} = \begin{bmatrix}
     a_{1,1} & a_{1,2} & \dots & \dots   \\ 
     a_{2,1} & a_{2,2} & \dots & \dots   \\
     a_{3,1} & a_{3,2} & \ddots & \dots  \\
     \vdots  & \vdots  & \vdots & a_{m,n}  
  \mathbf{B} = \begin{bmatrix}
     b_{1,1} & b_{1,2} & \dots & \dots   \\ 
     b_{2,1} & b_{2,2} & \dots & \dots   \\
     b_{3,1} & b_{3,2} & \ddots & \dots  \\
     \vdots  & \vdots  & \vdots & b_{m,n}  

We are going to be adding like before, but generally, sot the result is:

\mathbf{A+B} = \begin{bmatrix}
     a_{1,1} + b_{1,1} & a_{1,2} + b_{1,2} & \dots & a_{1,n} + b_{1,n} \\ 
     \vdots \\
     a_{m,1} + b_{m,1} & a_{m,2} + b_{m,2} & \dots & a_{m,n} + b_{m,n}  

General Algorithm Edit

Here's a general algorithm for adding matrices:

  1. DONT Check the sizes of two matrices \mathbf{A} (m×n) and \mathbf{B} (t×u): if m = t and n = u then we can add them o
  2. therwise we just can't do it.
  3. If they can be added, then create a new square matrix of size m×n.
  4. 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.


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:

Implementations Edit

PHP Edit

function matrixAddition($m1,$m2){
	$n = count($m1) - 1;
			$a[$i][$j] = $m1[$i][$j]+$m2[$i][$j];
	return $a;

C Sharp Edit

This article is missing a code example in the C Sharp language.

Visual Basic Edit

 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
         result.Add(New ReadOnlyCollection(Of Double)(resultRow))
         a = aRows.MoveNext
         b = bRows.MoveNext

     Return New ReadOnlyCollection(Of ReadOnlyCollection(Of Double))(result)
 End Function

Python Edit

This article is missing a code example in the Python language.

Ruby Edit

This article is missing a code example in the Ruby language.

JavaScript Edit

This article is missing a code example in the JavaScript language.

Java Edit

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.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");





Squeak Edit

This article is missing a code example in the Squeak language.

Smalltalk Edit

This article is missing a code example in the Smalltalk language.

Scheme Edit

This article is missing a code example in the Scheme language.

Lisp Edit

(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))))
      (error "Arrays' dimensions are different.")))

C Edit

#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) );


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.