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

#Region "Subtract Matrices"
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
' Subtracts two matrices from each other, their
' dimensions should be compatible!
' Function returns the solution or errors due to
' dimensions incompatibility

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
Public Shared Function Subtract(ByVal Mat1(,) As Double, ByVal Mat2(,) As Double) As Double(,)
Dim i, j As Integer
Dim sol(,) As Double
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("5007", , "Dimensions of the two matrices do not match !")

Error_Handler:
If Err.Number = 5007 Then
Err.Raise("5007", , "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