# How can I sum each element in two array? • ### Question

• Hi, I have a question about array calculation. Is there any function that I can sum elements between two array? I mean I have two array namely A and B.(Dimension of the two array is match.) I want to calculate

C(1) = A(1) + B(1)

from the first element to the last element in the two array. It is very simple and basic calculation, so I guess there is a simple funtion to do that calculation.(for example, mmult function in array product) Please let me know how to do that simple calculation.

Thursday, December 13, 2018 10:51 AM

• You are getting a "bug" because you are defining the dimensions of the arrays in the same subroutine as where you are doing the calculations:

Dim IptMatA(3, 2), IptMatB(2) As Variant

You can't then try to access IptMatA(m).

You need to split the code up into functions, for example for IptMatA:

```Option Base 1
Sub MatProductTest()
Dim IptMatA(3, 2), IptMatB(2) As Variant
Dim MatA, MatB As Variant
Dim RowA, ColA As Long, RowB, ColB As Long, i As Long, j As Long, k As Long
Dim MatC As Variant
Dim MatAConvert, MatBConvert As Variant
Dim m, n As Long
'From here Value assign to check the code
IptMatA(1, 1) = 3
IptMatA(1, 2) = 1
IptMatA(2, 1) = 4
IptMatA(2, 2) = 0
IptMatA(3, 1) = 1
IptMatA(3, 2) = -1
IptMatB(1) = 0
IptMatB(2) = 1

RowA = UBound(IptMatA, 1)

' Move this code into a function
'    If GetArrayD(IptMatA) = 1 Then
'        ColA = 1
'        'Convert 1-dimension array IptMatA to 2-dimension array MatA
'        ReDim MatAConvert(1 To RowA, 1)
'        For m = 1 To RowA
'            MatAConvert(m, 1) = IptMatA(m)
'        Next m
'        MatA = MatAConvert
'    Else
'        ColA = UBound(IptMatA, 2)
'        MatA = IptMatA
'    End If
MatA = Ensure2Dimensions(IptMatA)
ColA = UBound(IptMatA, 2)
MatA = IptMatA

If UBound(IptMatB, 1) <> ColA Then
'Mat2Product = "Prduct dimension Not Match!"
Exit Sub
End If

RowB = UBound(IptMatB, 1)

If GetArrayD(IptMatB) = 1 Then
ColB = 1
'Convert 1-dimension array IptMatB to 2-dimension array MatB
ReDim MatBConvert(1 To RowB, 1)
For n = 1 To RowB
MatBConvert(n, 1) = IptMatB(n)
Next n
MatB = MatBConvert
Else
ColB = UBound(IptMatB, 2)
MatB = IptMatB
End If
ReDim C(1 To RowA, 1 To ColB)
For i = 1 To RowA
For j = 1 To ColB
For k = 1 To ColA
MatC(i, j) = MatC(i, j) + MatA(i, k) * MatB(k, j)
Next k
Next j
Next i

'Mat2Product = C

End Sub

Function GetArrayD(MatA As Variant) As Long
Dim ElementCount, dimension As Long
Dim Element As Variant
ElementCount = 0
For Each Element In MatA
ElementCount = ElementCount + 1

Next Element

dimension = ElementCount / (UBound(MatA, 1) - LBound(MatA, 1) + 1)
GetArrayD = dimension
End Function

Function Ensure2Dimensions(MatA As Variant) As Variant
Dim MatAConvert() As Variant
If GetArrayD(MatA) = 1 Then
'Convert 1-dimension array IptMatA to 2-dimension array MatA
ReDim MatAConvert(1 To RowA, 1)
For m = 1 To RowA
MatAConvert(m, 1) = MatA(m)
Next m
Ensure2Dimensions = MatAConvert
Else
Ensure2Dimensions = IptMatA
End If
End Function
```

• Proposed as answer by Saturday, December 15, 2018 4:37 PM
• Marked as answer by Tuesday, January 8, 2019 10:01 AM
Friday, December 14, 2018 3:45 AM

### All replies

• You'll have to loop through the elements:

Dim i As Long
For i = LBound(A) To UBound(A)
C(i) = A(i) + B(i)
Next i

Regards, Hans Vogelaar (http://www.eileenslounge.com)

Thursday, December 13, 2018 11:51 AM
• Of coursly I know how to do the calculation with the Loop. But I want to do that regardless of dimension of

the two array. Here is code I am working on that problem

==============================================

Option Explicit

Option Base 1

Sub MatProductTest()

Dim IptMatA(3, 2), IptMatB(2) As Variant
Dim MatA, MatB As Variant
Dim RowA, ColA As Long, RowB, ColB As Long, i As Long, j As Long, k As Long
Dim MatC As Variant
Dim MatAConvert, MatBConvert As Variant
Dim m, n As Long

'From here Value assign to check the code

IptMatA(1, 1) = 3
IptMatA(1, 2) = 1
IptMatA(2, 1) = 4
IptMatA(2, 2) = 0
IptMatA(3, 1) = 1
IptMatA(3, 2) = -1

IptMatB(1) = 0
IptMatB(2) = 1

'If TypeName(IptMatA) = "Range" Then IptMatA = IptMatA.Value
'If TypeName(IptMatB) = "Range" Then IptMatB = IptMatB.Value

RowA = UBound(IptMatA, 1)
'i = GetArrayD(IptMatA)

'code check value assign end

If GetArrayD(IptMatA) = 1 Then
ColA = 1
'Convert 1-dimension array IptMatA to 2-dimension array MatA
ReDim MatAConvert(1 To RowA, 1)
For m = 1 To RowA
MatAConvert(m, 1) = IptMatA(m)
Next m
MatA = MatAConvert
Else
ColA = UBound(IptMatA, 2)
MatA = IptMatA
End If

If UBound(IptMatB, 1) <> ColA Then
'Mat2Product = "Prduct dimension Not Match!"
Exit Sub
End If

RowB = UBound(IptMatB, 1)

If GetArrayD(IptMatB) = 1 Then
ColB = 1
'Convert 1-dimension array IptMatB to 2-dimension array MatB
ReDim MatBConvert(1 To RowB, 1)
For n = 1 To RowB
MatBConvert(n, 1) = IptMatB(n)
Next n
MatB = MatBConvert
Else
ColB = UBound(IptMatB, 2)
MatB = IptMatB
End If

ReDim C(1 To RowA, 1 To ColB)
For i = 1 To RowA
For j = 1 To ColB
For k = 1 To ColA
MatC(i, j) = MatC(i, j) + MatA(i, k) * MatB(k, j)
Next k
Next j
Next i

'Mat2Product = C

End Sub

Function GetArrayD(MatA As Variant) As Long

Dim ElementCount, dimension As Long
Dim Element As Variant

ElementCount = 0

For Each Element In MatA

ElementCount = ElementCount + 1

Next Element

dimension = ElementCount / (UBound(MatA, 1) - LBound(MatA, 1) + 1)

GetArrayD = dimension

End Function

===================================================

In this code IptMatA(m) causes debug error when the dimension of IptMatA(m) is not 1-dimension.(Even the variable is in the If structure so that don't have to be used in that case.)

I'm trying to do that simple calculation regardless of dimension of array. I'll be appreciated If anyone can help me. Thanks

• Edited by Friday, December 14, 2018 1:32 AM
Friday, December 14, 2018 12:38 AM
• You are getting a "bug" because you are defining the dimensions of the arrays in the same subroutine as where you are doing the calculations:

Dim IptMatA(3, 2), IptMatB(2) As Variant

You can't then try to access IptMatA(m).

You need to split the code up into functions, for example for IptMatA:

```Option Base 1
Sub MatProductTest()
Dim IptMatA(3, 2), IptMatB(2) As Variant
Dim MatA, MatB As Variant
Dim RowA, ColA As Long, RowB, ColB As Long, i As Long, j As Long, k As Long
Dim MatC As Variant
Dim MatAConvert, MatBConvert As Variant
Dim m, n As Long
'From here Value assign to check the code
IptMatA(1, 1) = 3
IptMatA(1, 2) = 1
IptMatA(2, 1) = 4
IptMatA(2, 2) = 0
IptMatA(3, 1) = 1
IptMatA(3, 2) = -1
IptMatB(1) = 0
IptMatB(2) = 1

RowA = UBound(IptMatA, 1)

' Move this code into a function
'    If GetArrayD(IptMatA) = 1 Then
'        ColA = 1
'        'Convert 1-dimension array IptMatA to 2-dimension array MatA
'        ReDim MatAConvert(1 To RowA, 1)
'        For m = 1 To RowA
'            MatAConvert(m, 1) = IptMatA(m)
'        Next m
'        MatA = MatAConvert
'    Else
'        ColA = UBound(IptMatA, 2)
'        MatA = IptMatA
'    End If
MatA = Ensure2Dimensions(IptMatA)
ColA = UBound(IptMatA, 2)
MatA = IptMatA

If UBound(IptMatB, 1) <> ColA Then
'Mat2Product = "Prduct dimension Not Match!"
Exit Sub
End If

RowB = UBound(IptMatB, 1)

If GetArrayD(IptMatB) = 1 Then
ColB = 1
'Convert 1-dimension array IptMatB to 2-dimension array MatB
ReDim MatBConvert(1 To RowB, 1)
For n = 1 To RowB
MatBConvert(n, 1) = IptMatB(n)
Next n
MatB = MatBConvert
Else
ColB = UBound(IptMatB, 2)
MatB = IptMatB
End If
ReDim C(1 To RowA, 1 To ColB)
For i = 1 To RowA
For j = 1 To ColB
For k = 1 To ColA
MatC(i, j) = MatC(i, j) + MatA(i, k) * MatB(k, j)
Next k
Next j
Next i

'Mat2Product = C

End Sub

Function GetArrayD(MatA As Variant) As Long
Dim ElementCount, dimension As Long
Dim Element As Variant
ElementCount = 0
For Each Element In MatA
ElementCount = ElementCount + 1

Next Element

dimension = ElementCount / (UBound(MatA, 1) - LBound(MatA, 1) + 1)
GetArrayD = dimension
End Function

Function Ensure2Dimensions(MatA As Variant) As Variant
Dim MatAConvert() As Variant
If GetArrayD(MatA) = 1 Then
'Convert 1-dimension array IptMatA to 2-dimension array MatA
ReDim MatAConvert(1 To RowA, 1)
For m = 1 To RowA
MatAConvert(m, 1) = MatA(m)
Next m
Ensure2Dimensions = MatAConvert
Else
Ensure2Dimensions = IptMatA
End If
End Function
```

• Proposed as answer by Saturday, December 15, 2018 4:37 PM
• Marked as answer by Tuesday, January 8, 2019 10:01 AM
Friday, December 14, 2018 3:45 AM