如何找到局部最大值

问题描述 投票:0回答:1

我编写了一个 VBA 代码来查找某些情况下的局部最大值具有“负面影响”的数据图并且它工作得很好。然而,在稍微改变的情况下,它给了我最大值,(不是圆圈中的值)没有“负面影响”的数据图,我不知道如何更改代码以给我需要的值(圆圈里的那个)?

我尝试用导数和斜率做一些事情,但没有成功。 这是没有衍生的原始代码:

Option Explicit

Sub FindFirstPeakInSheet(wsSource As Worksheet, ByRef firstPeak As Double, ByRef foundPeak As Boolean)
    Dim lastRow As Long
    Dim i As Long

    ' Initialize the foundPeak flag to False
    foundPeak = False

    ' Find the last row with data in column B
    lastRow = wsSource.Cells(wsSource.Rows.Count, 2).End(xlUp).Row

    ' Clear the auxiliary column (column C)
    wsSource.Range("C1:C" & lastRow).ClearContents

    ' Loop through the range in column B and fill auxiliary column with peaks
    For i = 2 To lastRow - 1
        If IsNumeric(wsSource.Cells(i, 2).Value) And _
           IsNumeric(wsSource.Cells(i - 1, 2).Value) And _
           IsNumeric(wsSource.Cells(i + 1, 2).Value) Then

            If wsSource.Cells(i, 2).Value > wsSource.Cells(i - 1, 2).Value And _
               wsSource.Cells(i, 2).Value > wsSource.Cells(i + 1, 2).Value And _
               wsSource.Cells(i, 2).Value > 2 Then
                wsSource.Cells(i, 3).Value = wsSource.Cells(i, 2).Value ' Fill auxiliary column with peaks
            End If
        End If
    Next i

    ' Find the first peak in the auxiliary column
    For i = 2 To lastRow
        If IsNumeric(wsSource.Cells(i, 3).Value) And wsSource.Cells(i, 3).Value <> "" Then
            firstPeak = wsSource.Cells(i, 3).Value
            foundPeak = True
            Exit For
        End If
    Next i
End Sub



Sub FindFirstPeakTest2()
    Dim wsSource As Worksheet
    Dim wsResults As Worksheet
    Dim firstPeak As Double
    Dim foundPeak As Boolean
    Dim resultRow As Long

    ' Set the results sheet (change "Results" to your desired results sheet name)
    Set wsResults = ThisWorkbook.Sheets("Results")

    ' Initialize the result row for output in the Results sheet, starting from D3
    resultRow = 3

    ' Loop through each worksheet in the workbook except the Results sheet and the specified sheets
    For Each wsSource In ThisWorkbook.Worksheets
        If wsSource.Name <> "Results" And wsSource.Name <> "Parameters" And wsSource.Name <> "Statistics" Then
            ' Call the subroutine to find the first peak in the current sheet
            Call FindFirstPeakInSheet(wsSource, firstPeak, foundPeak)

            ' Output the first peak or a message if no peak found
            If foundPeak Then
                wsResults.Cells(resultRow, 4).Value = firstPeak ' Output the first peak in column D
            Else
                wsResults.Cells(resultRow, 4).Value = "No peak found"
            End If

            ' Move to the next row for the next sheet's result
            resultRow = resultRow + 1
        End If
    Next wsSource
End Sub

如果您对如何更改代码或编写新代码有一些想法或建议,欢迎分享。非常感谢!

excel vba performance plot max
1个回答
0
投票

没有测试数据,但试试这个。我假设

x
点间隔均匀。
在辅助栏
C
中,计算:

    wsSource.Cells(i, 3).Value = 2 * wsSource.Cells(i, 2).Value
        - wsSource.Cells(i - 1, 2).Value - wsSource.Cells(i + 1, 2).Value

并寻找此列中的最大值。

© www.soinside.com 2019 - 2024. All rights reserved.