تابعي براي جمع كردن دو ماتريس


#Region "Add Matrices"
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
' Add two matrices, their dimensions should be compatible!
' Function returns the summation or errors due to
' dimensions incompatibility

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
Public Shared Function Add(ByVal Mat1(,) As Double, ByVal Mat2(,) As Double) As Double(,)
Dim sol(,) As Double
Dim i, j As Integer
Dim Rows1, Cols1 As Integer
Dim Rows2, Cols2 As Integer

On Error GoTo Error_Handler

Find_R_C(Mat1, Rows1, Cols1)
Find_R_C(Mat2, Rows2, Cols2)

If Rows1 <> Rows2 Or Cols1 <> Cols2 Then
GoTo Error_Dimension
End If

ReDim sol(Rows1, Cols1)
For i = 0 To Rows1
For j = 0 To Cols1
sol(i, j) = Mat1(i, j) + Mat2(i, j)
Next j
Next i

Return sol

Error_Dimension:
Err.Raise("5005", , "Dimensions of the two matrices do not match !")

Error_Handler:
If Err.Number = 5005 Then
Err.Raise("5005", , "Dimensions of the two matrices do not match !")
Else
Err.Raise("5022", , "One or both of the matrices are null, this operation cannot be done !!")
End If

End Function
#End Region