===== Matrix Addition =====

If you need some background [[http://en.wikipedia.org/wiki/Matrix_%28mathematics%29|Go here]]

==== Basics ==== 2 matrices can be added only if they have the same dimensions. The result will be a third matrix of the same dimensions.
To perform the addition, numbers in matching postions in the 2 input matrices are added and the result is placed in the same position in the output matrix.

=== Example: Adding 2x2 Matrices === 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 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.

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 ====

Here's a general algorithm for adding matrices:

==== Pseudocode ====

The following psedocode adds matrices of size m×n.

addMatrix(matrixA, matrixB)

C = new Matrix( matrixA.NumberOfRows, matrixA.NumberOfColumns ) 

for r = 1 to matrixA.NumberOfRows
    for c = 1 to matrixA.NumberOfColumns
        C (r , c) = A(r , c) + B(r , c)
    next c
next r

return C  

end

<\/code>

===== Implementations =====

==== C# ====

==== VB ====

==== Python ====

==== Ruby ====

==== Javascript ====

==== Java ====

==== Squeak / Smalltalk ====

==== Scheme / Lisp ====

==== C ====

[[Category:Algorithms]]