Filter löschen
Filter löschen

Find max and min values, seperated by max or min

1 Ansicht (letzte 30 Tage)
FV
FV am 20 Apr. 2020
Kommentiert: KSSV am 20 Apr. 2020
I have a large set of numbers and need to find two minimum values and two maximum values. However it shouldn't just be the two absolute max and min values they need to be seperated by a max or min value. So max,min,max,min.

Akzeptierte Antwort

KSSV
KSSV am 20 Apr. 2020
Bearbeitet: KSSV am 20 Apr. 2020
Let A be your array.
[val1,idx1] = max(A) ;
A(idx1) = NaN ;
[val2,idx2] = max(A) ;
if idx1 > idx2
[val3,idx3] = min(A(idx2:idx1)) ;
else
[val3,idx3] = min(A(idx1:idx2)) ;
end
[val4,idx4] = min(A(idx2:end)) ;
Also read about findpeaks. Also you can sort array and get it.
  6 Kommentare
FV
FV am 20 Apr. 2020
no, it won't be any value between them, that's the problem. I want to exlude max values that is beside each other.
I made an example, so if A=[1;3;1.2;1.2;0.7;0.6;0.61;1;4;7;1.2;0.5]; The two max values is beside each other. I don't want that. So instead of 7 and 4 is max I want 7 and 3.
I also want the code to work if they are not beside each other. Just if the two values is next to each other I want to find the next value.
KSSV
KSSV am 20 Apr. 2020
clc; clear all ;
A=[1;3;1.2;1.2;0.7;0.6;0.61;1;4;7;1.2;0.5];
B = A ;
[val1,idx1] = max(A) ;
A(idx1) = NaN ;
[val2,idx2] = max(A) ;
while (abs(idx1-idx2)==1)
A(idx2) = NaN ;
[val2,idx2] = max(A) ;
end
if idx1 > idx2
[val3,idx3] = min(A(idx2:idx1)) ;
else
[val3,idx3] = min(A(idx1:idx2)) ;
end
[val4,idx4] = min(A(idx2:end)) ;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink PLC Coder finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by