How to note the time of specific output in MATLAB?

2 views (last 30 days)
tic
Time = 0;
for ii = 1:n
if Time >= 20
break
end
Output = ....
Time = toc;
end
The output ranges from 1 to 10.
I want to execute code for 20 sec.
But witin 20 sec I want to note the time of some specific outputs when occur first time.
e.g., there is a chance that 2 occurs multiple times, but I want to note the time when 2 first time occur as an output within 20 seconds like wise 6, 8, and 10.
Furthermore, the output comes in an increasing order that is if 2 comes, then after that the number 2 0r greater than 2 comes, can not be less than 2.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Oct 2022
Edited: Walter Roberson on 21 Oct 2022
neverseen = true(1,10);
seenwhen = nan(1,10);
tic
Time = 0;
for ii = 1:n
if Time >= 20
break
end
Output = ....
Time = toc;
if neverseen(Output)
seenwhen(Output) = Time;
neverseen(Output) = false;
end
end
Now if neverseen(6) is true (for example) then that means that you did not receive any 6's and that seenwhen(6) [or whatever] is not valid. But if neverseen(6) is false then that means that you did seen a 6 on output and that seenwhen(6) tells you the time at which you saw it.
Question: is it useful to you to know the iteration number at which the output was seen, or only the elapsed time?
  2 Comments
Noor Fatima
Noor Fatima on 21 Oct 2022
@Walter Roberson Thank you very much for your response.
Question: is it useful to you to know the iteration number at which the output was seen, or only the elapsed time?
Yes, it will be
Walter Roberson
Walter Roberson on 21 Oct 2022
neverseen = true(1,10);
seenwhen = nan(1,10);
seeniter = nan(1,10);
tic
Time = 0;
for ii = 1:n
if Time >= 20
break
end
Output = ....
Time = toc;
if neverseen(Output)
seenwhen(Output) = Time;
seeniter(Output) = ii;
neverseen(Output) = false;
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!