Find a value when it meets two conditions
Afficher commentaires plus anciens
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
dpb
le 27 Juin 2021
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
le 27 Juin 2021
dpb
le 27 Juin 2021
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
le 1 Juil 2021
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!