[Redefining Matlab Powerful Series 8] Use matlab to find local values (function islocalmax to find local maximum + function islocalmin to find local minimum)

Operating environment: Matlab

Written by: Left Hand の Tomorrow

Featured column: “python”

Recommended column: “Algorithm Research”

#### Anti-counterfeiting watermark–Left hand の tomorrow ####

Hello everyone, I am Left Hand の Tomorrow! haven’t seen you for a long time

Open a new series today–Redefining the powerful series of matlab

Last updated: May 21, 2023, 284 original blog of Left Hand の Tomorrow

Updated in column: matlab

#### Anti-counterfeiting watermark–Left Hand の Tomorrow ####

matlab interactive task

The Find Local Extrema task interactively finds the local maximum and local minimum of the data. This task automatically generates MATLAB code for live scripts.

With this task, you can:

  • Finds local maxima, local minima, or both for data contained by workspace variables.

  • Adjust parameters to find fewer or more extreme values.

  • Visualize detected extrema.

To add the Find Local Extrema task to a live script in the MATLAB Editor:

  • In the Live Editor tab, select Task > Find Local Extrema.

  • In the script’s code block, type the relevant keyword, such as extrema or find. Select Find Local Extrema from the suggested command autocomplete items.

islocalmin – calculate local minimum

(1) Syntax

TF = islocalmin(A) will return element 1 when a local minimum is detected in the corresponding element of A ( true) logical array.

TF = islocalmin(A,dim) specifies the dimension of A along which to operate. For example, islocalmin(A,2) finds the local minimum for each row in matrix A .

TF = islocalmin(___,Name,Value) One or more name-value parameters can be used in addition to any combination of input parameters in the previous syntax to specify other Parameters for local minima. For example, islocalmin(A,'SamplePoints',t) computes the local minimum of A with respect to the timestamps contained in the time vector t .

[TF,P] = islocalmin(___) also returns the relative elevation difference corresponding to each element of A.

Relative height difference of local minima

The relative height difference of a local minimum (or trough) measures how prominent that trough is based on its depth and position relative to other troughs.

To measure the relative height difference of a trough, first extend a horizontal line from the trough. Find the intersection point of this line with the data on the left and right sides, which should be another trough or the endpoint of the data. Mark these locations as the outer endpoints of the left and right intervals. Then find the highest peak in the left and right intervals. Take the smaller of these two peaks and measure the vertical distance from that peak to the trough. This distance is the relative elevation difference.

For the vector x, the maximum relative height difference does not exceed max(x)-min(x).

(2) Example

Local minima in vector

Compute and plot local minima of a data vector.

x = 1:100;
A = (1-cos(2*pi*0.01*x)).*sin(2*pi*0.15*x);
TF = islocalmin(A);
plot(x,A,x(TF),A(TF),'r*')

Minimum value in matrix row

Create a data matrix, then compute the local minima for each row.

A = -25*diag(ones(5,1)) + rand(5,5);
TF = islocalmin(A,2)

TF = 5x5 logical array

   0 0 0 1 0
   0 1 0 0 0
   0 0 1 0 0
   0 0 0 1 0
   0 0 1 0 0

Minimum value apart

Computes the local minimum of a data vector with respect to the timestamps in the vector t . Use the MinSeparation parameter to calculate minimum values that are at least 45 minutes apart.

t = hours(linspace(0,3,15));
A = [2 4 6 4 3 7 5 6 5 10 4 -1 -3 -2 0];
TF = islocalmin(A,'MinSeparation',minutes(45),'SamplePoints',t);
plot(t,A,t(TF),A(TF),'r*')

Minimum platform area

Specifies the method used to indicate consecutive minimum values.

Computes local minima for data containing consecutive minima. Indicates the first minimum for each plateau zone.

x = 0:0.1:5;
A = max(-0.75, sin(pi*x));
TF1 = islocalmin(A,'FlatSelection','first');
plot(x,A,x(TF1),A(TF1),'r*')

Indicates all minimum values for each plateau zone.

TF2 = islocalmin(A,'FlatSelection','all');
plot(x,A,x(TF2),A(TF2),'r*')

Highlight min

Compute local minima and relative height differences for a vector of data, then plot with the data.

x = 1:100;
A = peaks(100);
A = A(50,:);
[TF1,P] = islocalmin(A);
P(TF1)

ans = 1×2

    2.7585 1.7703

plot(x,A,x(TF1),A(TF1),'r*')
axis tight

Computes the most prominent minimum in the data by specifying a minimum relative height difference requirement.

TF2 = islocalmin(A,'MinProminence',2);
plot(x,A,x(TF2),A(TF2),'r*')
axis tight

islocalmax – calculate local maximum

(1) Grammar

TF = islocalmax(A) will return the element as 1 when a local maximum is detected in the corresponding element of A ( true) logical array.

TF = islocalmax(A,dim) specifies the dimension of A along which to operate. For example, islocalmax(A,2) computes the local maximum for each row of matrix A .

TF = islocalmax(___,Name,Value) One or more name-value parameters can be used in addition to any combination of input parameters in the previous syntax to specify other Parameters for local maxima. For example, islocalmax(A,'SamplePoints',t) computes the local maximum of A from the timestamps contained in the time vector t .

[TF,P] = islocalmax(___) also returns the relative elevation difference corresponding to each element of A.

(2) Example

Local maxima in vector

Compute and plot local maxima of a data vector.

x = 1:100;
A = (1-cos(2*pi*0.01*x)).*sin(2*pi*0.15*x);
TF = islocalmax(A);
plot(x,A,x(TF),A(TF),'r*')

Maximum value in matrix row

Create a data matrix, then compute the local maximum for each row.

A = 25*diag(ones(5,1)) + rand(5,5);
TF = islocalmax(A,2)

TF = 5x5 logical array

   0 0 1 0 0
   0 1 0 0 0
   0 0 1 0 0
   0 1 0 1 0
   0 1 0 0 0

Maximum value apart

Computes the local maximum of a data vector with respect to the timestamps in the vector t . Use the MinSeparation parameter to calculate the maximum values separated by at least 45 minutes.

t = hours(linspace(0,3,15));
A = [2 4 6 4 3 7 5 6 5 10 4 -1 -3 -2 0];
TF = islocalmax(A,'MinSeparation',minutes(45),'SamplePoints',t);
plot(t,A,t(TF),A(TF),'r*')

Maximum platform area

Specifies the method used to indicate the continuous maximum value.

Computes local maxima for data containing consecutive maxima. Indicates the first maximum value for each plateau zone.

x = 0:0.1:5;
A = min(0.75, sin(pi*x));
TF1 = islocalmax(A,'FlatSelection','first');
plot(x,A,x(TF1),A(TF1),'r*')

Indicates all maximum values for each plateau zone.

TF2 = islocalmax(A,'FlatSelection','all');
plot(x,A,x(TF2),A(TF2),'r*')

Prominent maximum values

Choose the maximum value based on the relative height difference.

Compute local maxima and relative height differences for a vector of data, then plot with the data.

x = 1:100;
A = peaks(100);
A = A(50,:);
[TF1,P] = islocalmax(A);
P(TF1)


ans = 1×2

    1.7703 3.5548
plot(x,A,x(TF1),A(TF1),'r*')
axis tight

By specifying a minimum relative height difference requirement, only the most prominent maxima in the data are calculated.

TF2 = islocalmax(A,'MinProminence',2);
plot(x,A,x(TF2),A(TF2),'r*')
axis tight

#### Anti-counterfeiting watermark–Left hand の tomorrow ####

Hello everyone, I am Left Hand の Tomorrow! haven’t seen you for a long time

Open a new series today–Redefining the powerful series of matlab

Last updated: May 21, 2023, 284 original blog of Left Hand の Tomorrow

Updated in column: matlab

#### Anti-counterfeiting watermark–Left Hand の Tomorrow ####