数组的累加和
Array's cumulative sum
我有一个整数值数组,想找到一种计算其累积总和的简单方法 (S = Data(1) + Data(2) + ... + Data(x)
)。
我已经创建了这个函数:
Function CumulativeSum(Data() As Integer, k As Integer) As Integer
For entry = 1 To k
CumulativeSum = CumulativeSum + Data(entry)
Next entry
End Function
并且运行良好。但是,我想知道是否有更好的方法(主要是不使用任何额外函数,基本上只使用 excel 函数,如 Application.Sum
)。我在网上做了一个小搜索,但在此基础上没有找到任何东西。
我知道我不是要更正任何代码,我只是要求一个替代方案,这不是本论坛的真正目的。但是,我也怀疑答案可能很简单,所以...如果有人愿意帮助我,我将非常非常感激!如果您找到类似问题的答案,请与我分享 link,我会删除这个。
我很抱歉我可能没有明确要求:我只是想找到一种简单的方法来计算累积和,使用宏例程本身的简单函数,而不使用 CumulativeSum
我创建的函数或用户创建的任何其他函数。
如果你想从Array(a,b,c)得到一个像Array(a,a+b,a+b+c)这样的累积数组数组,那么这是实现它的功能,如果你想传递开始和结束参数:
Public Sub TestMe()
Dim outputArray As Variant
Dim inputArray As Variant
Dim counter As Long
inputArray = Array(1, 2, 4, 8, 16, 32, 64)
outputArray = generateCumulativeArray(inputArray, 1, 4)
For counter = LBound(outputArray) To UBound(outputArray)
Debug.Print outputArray(counter)
Next counter
outputArray = generateCumulativeArray(inputArray, toValue:=4)
For counter = LBound(outputArray) To UBound(outputArray)
Debug.Print outputArray(counter)
Next counter
End Sub
Public Function generateCumulativeArray(dataInput As Variant, _
Optional fromValue As Long = 0, _
Optional toValue As Long = 0) As Variant
Dim i As Long
Dim dataReturn As Variant
ReDim dataReturn(0)
dataReturn(0) = dataInput(fromValue)
For i = 1 To toValue - fromValue
ReDim Preserve dataReturn(i)
dataReturn(i) = dataReturn(i - 1) + dataInput(fromValue + i)
Next i
generateCumulativeArray = dataReturn
End Function
关于只是对数组求和,这是这样做的方法:
您可以使用 WorksheetFunction.
并且可以将数组作为参数传递。因此,您可以获得所有功能,例如Average
、Min
、Max
等:
Option Explicit
Public Sub TestMe()
Dim k As Variant
k = Array(2, 10, 200)
Debug.Print WorksheetFunction.Sum(k)
Debug.Print WorksheetFunction.Average(k)
End Sub
如果您想要从给定起点到给定终点的总和,最简单的方法可能是创建一个新数组并将其完全求和。在 Python 中,这称为切片,在 VBA 中,这可以手动完成:
Public Sub TestMe()
Dim varArr As Variant
Dim colSample As New Collection
varArr = Array(1, 2, 4, 8, 16, 32, 64)
colSample.Add (1)
colSample.Add (2)
colSample.Add (4)
colSample.Add (8)
Debug.Print WorksheetFunction.Sum(generateArray(varArr, 2, 4))
Debug.Print WorksheetFunction.Sum(generateArray(colSample, 2, 4))
End Sub
Public Function generateArray(data As Variant, _
fromValue As Long, _
toValue As Long) As Variant
Dim i As Long
Dim dataInternal As Variant
Dim size As Long
size = toValue - fromValue
ReDim dataInternal(size)
For i = LBound(dataInternal) To UBound(dataInternal)
dataInternal(i) = data(i + fromValue)
Next i
generateArray = dataInternal
End Function
思路是generateArray
函数returns一个新数组。因此,它的总和就是您所需要的。它也适用于集合,而不仅仅是数组。注意,当使用集合时,它们从索引 1 开始,而数组(通常)从 0 开始。如果你想对数组和集合使用相同的索引,然后将 generateArray 函数更改为这个:
Public Function generateArray(data As Variant, _
fromValue As Long, _
toValue As Long) As Variant
Dim i As Long
Dim dataInternal As Variant
Dim size As Long
size = toValue - fromValue
ReDim dataInternal(size)
If IsArray(data) Then
For i = LBound(dataInternal) To UBound(dataInternal)
dataInternal(i) = data(i + fromValue)
Next i
Else
For i = LBound(dataInternal) To UBound(dataInternal)
dataInternal(i) = data(i + fromValue + 1)
Next i
End If
generateArray = dataInternal
End Function
或者在上面写Option Base 1
,数组将从1开始(不建议!)。
对于累计总和,请尝试以下操作
Function CumulativeSum(Data() As Integer, k As Integer) As Integer
Dim tempArr
tempArr = Data
ReDim Preserve temp(0 To k - 1)
CumulativeSum = WorksheetFunction.Sum(tempArr)
End Function
编辑:
Sub Demo()
Dim MyArray
Dim i As Long
MyArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
Debug.Print MyArray(LBound(MyArray))
For i = LBound(MyArray) + 1 To UBound(MyArray)
MyArray(i) = MyArray(i - 1) + MyArray(i)
Debug.Print MyArray(i)
Next i
End Sub
以上代码从
更新数组arr
1, 2, 3, 4, 5, 6, 7, 8, 9
至
1, 3, 6, 10, 15, 21, 28, 36, 45
试试这个:
Sub test()
Dim arr As Variant
arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Dim mySum As Long, k As Long
Dim wsf As WorksheetFunction
Set wsf = Application.WorksheetFunction
k = 6
'operative line below
mySum = wsf.Sum(wsf.Index(arr, 1, Evaluate("ROW(1:" & k & ")")))
MsgBox mySum
End Sub
此函数returns一个数组,其中包含原始向量的累加和。
Function CumuVector(Vec As Variant) As Variant()
Dim element, v() As Variant
Dim i As Integer
lastindexinvec = 0
For Each element In Vec
lastindexinvec = last + 1
Next
ReDim v(lastindexinvec) As Variant
i = 0
For Each element In Vec
If i < last Then
sum = sum + element
v(i) = sum
i = i + 1
End If
Next
CumuVector = v
End Function
我有一个整数值数组,想找到一种计算其累积总和的简单方法 (S = Data(1) + Data(2) + ... + Data(x)
)。
我已经创建了这个函数:
Function CumulativeSum(Data() As Integer, k As Integer) As Integer
For entry = 1 To k
CumulativeSum = CumulativeSum + Data(entry)
Next entry
End Function
并且运行良好。但是,我想知道是否有更好的方法(主要是不使用任何额外函数,基本上只使用 excel 函数,如 Application.Sum
)。我在网上做了一个小搜索,但在此基础上没有找到任何东西。
我知道我不是要更正任何代码,我只是要求一个替代方案,这不是本论坛的真正目的。但是,我也怀疑答案可能很简单,所以...如果有人愿意帮助我,我将非常非常感激!如果您找到类似问题的答案,请与我分享 link,我会删除这个。
我很抱歉我可能没有明确要求:我只是想找到一种简单的方法来计算累积和,使用宏例程本身的简单函数,而不使用 CumulativeSum
我创建的函数或用户创建的任何其他函数。
如果你想从Array(a,b,c)得到一个像Array(a,a+b,a+b+c)这样的累积数组数组,那么这是实现它的功能,如果你想传递开始和结束参数:
Public Sub TestMe()
Dim outputArray As Variant
Dim inputArray As Variant
Dim counter As Long
inputArray = Array(1, 2, 4, 8, 16, 32, 64)
outputArray = generateCumulativeArray(inputArray, 1, 4)
For counter = LBound(outputArray) To UBound(outputArray)
Debug.Print outputArray(counter)
Next counter
outputArray = generateCumulativeArray(inputArray, toValue:=4)
For counter = LBound(outputArray) To UBound(outputArray)
Debug.Print outputArray(counter)
Next counter
End Sub
Public Function generateCumulativeArray(dataInput As Variant, _
Optional fromValue As Long = 0, _
Optional toValue As Long = 0) As Variant
Dim i As Long
Dim dataReturn As Variant
ReDim dataReturn(0)
dataReturn(0) = dataInput(fromValue)
For i = 1 To toValue - fromValue
ReDim Preserve dataReturn(i)
dataReturn(i) = dataReturn(i - 1) + dataInput(fromValue + i)
Next i
generateCumulativeArray = dataReturn
End Function
关于只是对数组求和,这是这样做的方法:
您可以使用 WorksheetFunction.
并且可以将数组作为参数传递。因此,您可以获得所有功能,例如Average
、Min
、Max
等:
Option Explicit
Public Sub TestMe()
Dim k As Variant
k = Array(2, 10, 200)
Debug.Print WorksheetFunction.Sum(k)
Debug.Print WorksheetFunction.Average(k)
End Sub
如果您想要从给定起点到给定终点的总和,最简单的方法可能是创建一个新数组并将其完全求和。在 Python 中,这称为切片,在 VBA 中,这可以手动完成:
Public Sub TestMe()
Dim varArr As Variant
Dim colSample As New Collection
varArr = Array(1, 2, 4, 8, 16, 32, 64)
colSample.Add (1)
colSample.Add (2)
colSample.Add (4)
colSample.Add (8)
Debug.Print WorksheetFunction.Sum(generateArray(varArr, 2, 4))
Debug.Print WorksheetFunction.Sum(generateArray(colSample, 2, 4))
End Sub
Public Function generateArray(data As Variant, _
fromValue As Long, _
toValue As Long) As Variant
Dim i As Long
Dim dataInternal As Variant
Dim size As Long
size = toValue - fromValue
ReDim dataInternal(size)
For i = LBound(dataInternal) To UBound(dataInternal)
dataInternal(i) = data(i + fromValue)
Next i
generateArray = dataInternal
End Function
思路是generateArray
函数returns一个新数组。因此,它的总和就是您所需要的。它也适用于集合,而不仅仅是数组。注意,当使用集合时,它们从索引 1 开始,而数组(通常)从 0 开始。如果你想对数组和集合使用相同的索引,然后将 generateArray 函数更改为这个:
Public Function generateArray(data As Variant, _
fromValue As Long, _
toValue As Long) As Variant
Dim i As Long
Dim dataInternal As Variant
Dim size As Long
size = toValue - fromValue
ReDim dataInternal(size)
If IsArray(data) Then
For i = LBound(dataInternal) To UBound(dataInternal)
dataInternal(i) = data(i + fromValue)
Next i
Else
For i = LBound(dataInternal) To UBound(dataInternal)
dataInternal(i) = data(i + fromValue + 1)
Next i
End If
generateArray = dataInternal
End Function
或者在上面写Option Base 1
,数组将从1开始(不建议!)。
对于累计总和,请尝试以下操作
Function CumulativeSum(Data() As Integer, k As Integer) As Integer
Dim tempArr
tempArr = Data
ReDim Preserve temp(0 To k - 1)
CumulativeSum = WorksheetFunction.Sum(tempArr)
End Function
编辑:
Sub Demo()
Dim MyArray
Dim i As Long
MyArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
Debug.Print MyArray(LBound(MyArray))
For i = LBound(MyArray) + 1 To UBound(MyArray)
MyArray(i) = MyArray(i - 1) + MyArray(i)
Debug.Print MyArray(i)
Next i
End Sub
以上代码从
更新数组arr
1, 2, 3, 4, 5, 6, 7, 8, 9
至
1, 3, 6, 10, 15, 21, 28, 36, 45
试试这个:
Sub test()
Dim arr As Variant
arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Dim mySum As Long, k As Long
Dim wsf As WorksheetFunction
Set wsf = Application.WorksheetFunction
k = 6
'operative line below
mySum = wsf.Sum(wsf.Index(arr, 1, Evaluate("ROW(1:" & k & ")")))
MsgBox mySum
End Sub
此函数returns一个数组,其中包含原始向量的累加和。
Function CumuVector(Vec As Variant) As Variant()
Dim element, v() As Variant
Dim i As Integer
lastindexinvec = 0
For Each element In Vec
lastindexinvec = last + 1
Next
ReDim v(lastindexinvec) As Variant
i = 0
For Each element In Vec
If i < last Then
sum = sum + element
v(i) = sum
i = i + 1
End If
Next
CumuVector = v
End Function