Problem with threshold in if statement

조회 수: 3 (최근 30일)
Siegmund Nuyts
Siegmund Nuyts 2022년 10월 26일
댓글: Siegmund Nuyts 2022년 11월 2일
My goal is to determine the difference between troughs and peaks, and select the troughs/peaks with a difference bigger than 35.
Sometimes there is no difference bigger than 35. So now I'm writing an if statement that if there is no difference bigger than 35, that it can just be 0.
The issue that I have is that there are multiple values varying below and above the threshold so it's not correctly identifying the threshold.
It should select the first trough/peak with a difference bigger than 35 and if there is no difference bigger than 35, then it should just be 0.
Here is the code that I have:
P = load('Ps.mat');
Ps = P.Ps;
threshold = 35;
[mins, min_locs] = findpeaks(-Ps);
[maxs, max_locs] = findpeaks(Ps);
peaks = [maxs max_locs];
troughs = [mins min_locs];
sp = size (peaks);
st = size (troughs);
s = max(sp(1), st(1));
dif = [[peaks;zeros(abs([s 0] - sp))],[troughs;zeros(abs([s, 0]-st))]];
difs = [dif (dif(:,1)+dif(:,3))];
if difs(difs(:,5)>threshold,:);
[idx, ~] = find(difs(:,5)>threshold);
difs = difs(unique(idx),:);
thresh = difs(1,4);
else thresh = 0
end
thresh = 0
  댓글 수: 3
Jeffrey Clark
Jeffrey Clark 2022년 10월 27일
@Siegmund Nuyts, hard to say from what you gave but please note that the conditional if difs(difs(:,5)>threshold,:) will be true as long as none of the difs(difs(:,5)>threshold,:) are zero. With floating point data it is unlikely that any column would be zero when column 5>threshold so the if may always be executing. Did tou intend if difs(:,5)>threshold which would execute the if part when any row with column 5>threshold was found?
Siegmund Nuyts
Siegmund Nuyts 2022년 10월 27일
Thanks for the reply. I added the data.
The if statement should look for any data in column 5 that is above 35.
If none of the data is above 35, then the result can be 0.

댓글을 달려면 로그인하십시오.

채택된 답변

Jeffrey Clark
Jeffrey Clark 2022년 10월 29일
편집: Jeffrey Clark 2022년 10월 29일
@Siegmund Nuyts, since you are negating the values input to Find local maxima - MATLAB findpeaks (mathworks.com) when looking for your valleys the mins returned will be negated values that you must then negate when used. Also since you are only interested in the first difference of the find indexed difs you should just have find return the first and eliminate some code. Along with my first comment the code would change to:
P = load('Ps.mat');
Ps = P.Ps;
threshold = 35;
[mins, min_locs] = findpeaks(-Ps);
[maxs, max_locs] = findpeaks(Ps);
peaks = [maxs max_locs];
troughs = [-mins min_locs]; %negate values to match -Ps use
sp = size (peaks);
st = size (troughs);
s = max(sp(1), st(1));
dif = [[peaks;zeros(abs([s 0] - sp))],[troughs;zeros(abs([s, 0]-st))]];
difs = [dif (dif(:,1)+dif(:,3))];
idx = find(difs(:,5)>threshold,1); %only need the first
if ~isempty(idx) %saves doing the find twice
thresh = difs(idx,4);
else thresh = 0
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by