Find a value when it meets two conditions

Hi,
I'm trying to find a specific value in an array when it meets two conditions (1- as soon as it occur the first time, 2- if its longer than 2 seconds), then, I want the code to stop and give me the length from the beginng to that value that mets both conditions. Here is my code:
t= (2*2048);
for i = 1:length (M)
if W = find ((M >=5) & i > t)
break
W = length(i);
end
end
Thanks in advance for the help.

4 commentaires

The first is easy enough--
V=5; % don't bury magic numbers in code; use variables
i1=find(M>V,1); % first exceedance of target value
The second isn't clear what is wanted -- what is the time variable and/or the sample rate? The above is simply counting positions in the vector which would imply a dt of 1/2 sec which is awfully limiting way to write code/store data.
Also, as written, the condition can't be satisfied until I>=4096 so there's no point in even beginning the for loop until then -- and, it would miss the first condition as written if M>V the first time before then.
So, all in all, we don't have a precise-enough definition of the requirements to write unequivocable code to satisfy the constraints -- they're unclear as to intent.
Saad Alqahtani
Saad Alqahtani le 27 Juin 2021
Thanks for the response. I hope I can clarify my code a little bit more.
So, the sample rate is 2048.
My second condition is that if we find that V >= 5 and it lasts for more than 2 seconds time window from the first time we find it (which is the criteria that I want to follow). So, as soon as both conditions has met, I need it to stop and provide me the the output which is the length of the array from the beginning to when the criteria has met.
The thing is that I don't want it to stop when it just find that V>=5 the first time, I want to make sure it lasts for at least 2 seconds.
Thanks again.
Again, is it from the first >V crossing for 2 seconds whether M is continuously > V or not, or must it be an unbroken 2 seconds of M>V lasting at a minum of 2 seconds?
Is it known the condition will be true at some point if the latter or might it not last that long?
OK, the sample rate is 2048 Hz, then the number of elements to reach 2 sec is 4096 but that would have to start after the initial trigger point.
Is it possible to have more than one pulse stream -- that is, could you have M>V but not 2 sec of duration but then another, later sequence in the same signal does meet the criterion?
On the assumption that there is only one pulse present, then something like
i1=find(M>V,1); % trigger
ie=find(M(i1:end)<V,1); % end after trigger
i2=i1+ie; % signal end
isOK=(ie>=t); % test if ON length meets minimum
A function written to do the above search could then return either the found values and the flag variable or, if the duration were too short, an empty result for the other value depending on the wanted usage.
Saad Alqahtani
Saad Alqahtani le 1 Juil 2021
Thanks. That helps, although I might need to modify it a bit.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by