for loop for signal with changing conditions?

1 view (last 30 days)
Mirko Tomic
Mirko Tomic on 27 Jul 2022
Answered: Jan on 27 Jul 2022
Hello, i need to create a for loop for the following orange signal with the first 2 negative peaks then 2 positive and so on
the problem is the for loop should only process the negative ones and not the positive peaks, and i really need a loop here with an if statement and cannot work with diff, find functions etc. because i want to extract specific values only in the negative peaks
my code so far, but i still get the values for the whole signal
for i=length(MomentRechts)%orange signal
if MomentRechts(i)<0;
MomR = find(diff(MomentRechts)>1000);
Thanks for everykind of help :)

Answers (1)

Jan on 27 Jul 2022
Do you mean:
for i = 1:length(MomentRechts)
% Instead of:
for i´= length(MomentRechts)
This command processes the complete signal:
MomR = find(diff(MomentRechts)>1000);
Maybe you want:
if MomentRechts(i) < 0
index = find(MomentRechts(i:end) >= 0, 1);
MomR = find(diff(MomentRechts(i:index)) > 1000);
But then you process this for each negative element again. This does not look useful.
Find the negative parts at first. Then process them block by block, not element by element.

Community Treasure Hunt

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

Start Hunting!

Translated by