## FANDOM

406 Pages

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 \end{bmatrix} \mathbf{B} = \begin{bmatrix} 5 & 6 \\ -7 & -2 \end{bmatrix}$

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 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ -4 & 2 \end{bmatrix}$

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} \end{bmatrix} \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} \end{bmatrix}$

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} \end{bmatrix}$

### 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.

### 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 Edit

### PHP Edit

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;
}

### Visual Basic Edit

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
a2 = aCols.MoveNext
b2 = bCols.MoveNext
Loop
a = aRows.MoveNext
b = bRows.MoveNext
Loop

End Function

### Java Edit

import java.util.Scanner;

{
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();

}

}

}

### 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))))
sum)
(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;
}

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