我已将数据分类为用户定义的类型,其中Dy是测量日期,Tm是时间,pH是测量值。
Type pHData
Dy As Date
Tm As Date
pH As Single
End Type
现在我想按天排序数据(每天有不同数量的数据点),这样我就可以找到平均值,最小值,最大值等。我已经创建了一个独特日期的数组,现在我想为每个唯一日期选择值
有点像(抱歉语法不完美,但我希望你明白这一点):
For i = LBound(uniqueArr) to UBound(uniqueArr)
For j = LBound(pHData) to UBound(pHData)
if pHData.Dy(j)== uniqueArr(i)
'store in temp array to find mean, etc.'
else
Next i
Next j
有什么建议吗?
答案 0 :(得分:0)
是否有理由分别存储日期和时间信息?
它们都作为浮点数据处理,其中整数部分表示日期,而小数部分表示时间。
示例: 40589.5
40589代表当天(今天) 0.5表示时间(中午)
将此值转换为日期/时间格式,您将 15 / Feb / 2011 12:00 PM
答案 1 :(得分:0)
如果您只想要min,max,mean,那么您不需要将值存储在临时数组中。见下面的例子。另外,请注意编写pHData(j).Dy
,而不是pHData.Dy(j)
。
For i = LBound(uniqueArr) to UBound(uniqueArr)
' Re-initialise min, max, sum
phMin = VeryLargeNumber ' Choice of VeryLargeNumber depends on your application.
phMax = - VeryLargeNumber ' Make it beyond the range of possible pH values.
phSum = 0
phCount = 0
For j = LBound(pHData) to UBound(pHData)
With phData(j)
If .Dy== uniqueArr(i)
' These will be used to calculate the mean later
phCount = phCount + 1
phSum = phSum + .pH
' Is this the max or min so far?
If .pH > phMax Then
' This is the largest pH value encountered so far
phMax = .pH
ElseIf .pH < phMin Then
' This is the smallest pH value encountered so far
phMin = .pH
Else
' This pH value is neither the largest nor smallest encountered so far.
' Do nothing.
End If
Else
' This measurement was not taken on this date.
' Do nothing.
End If
End With
phMean = phSum / phCount
' Here goes some code to store this date's
' min, max, and mean somewhere convenient.
Next j
Next i
如果你真的想把东西放在临时数组中,那么代码会更混乱也更慢......我们走了:
Dim todaysValues() As [Whatever type you need]
For i = LBound(uniqueArr) to UBound(uniqueArr)
' First, count how many measurements were taken today.
phCount = 0
For j = LBound(pHData) to UBound(pHData)
If phData(j).Dy== uniqueArr(i)
phCount = phCount + 1
Else
' This measurement was not taken on this date.
' Do nothing.
End If
Next j
' Now resize the temp array and store today's measurements in it.
ReDim todaysValues(1 To phCount)
phCount = 0
For j = LBound(pHData) to UBound(pHData)
If phData(j).Dy== uniqueArr(i)
phCount = phCount + 1
todaysValues(phCount) = phData(j).pH
Else
' This measurement was not taken on this date.
' Do nothing.
End If
Next j
' Here goes some code to calculate statistics (min, max, etc.)
Next i
答案 2 :(得分:0)
我原以为我可以将日期信息与每天排序数据的唯一日期数组进行匹配。在这种情况下,我并不关心时间,我只是想利用这一天对数据进行分组。
类似的东西:
If phData(1).Dy == Day(1)
Then add phData(1).pH to tempArray
'and find the mean, max, min, etc of tempArray