Programmer's Wiki

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:

  1. DONT Check the sizes of two matrices (m×n) and (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:



function matrixAddition($m1,$m2){
	$n = count($m1) - 1;
			$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
         result.Add(New ReadOnlyCollection(Of Double)(resultRow))
         a = aRows.MoveNext
         b = bRows.MoveNext

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


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


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


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


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






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


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


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


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


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