MATLAB Answers

Combination of "for loop" and "if statement"

9 views (last 30 days)
Cynthia Lee
Cynthia Lee on 12 Mar 2021
Commented: Rena Berman on 29 Jun 2021 at 14:16
I have an assignment that requires me to use both 'for loop' and 'if statement'. However I have zero clue how to start. The question is something like:
machine runs at below 3 Knots means not working
machine runs at 3 to 5 Knots is average
machine runs at above 4 Knots is good
In my excel sheet I have one column for the time and another column matching it by side are the Knot values. I have a total of 400 plus data in my excel sheet.
How do i put this into codes when I was to find how long the machine spend running in total average speeed and how long running in good speed only?
Idk how to link the two coloumns tgt.
if ???
if (y<3)
display ('not working')
elseif(3<y<5)
display ('average')
else(y>5)
display('good')
end
end
  4 Comments
Rena Berman
Rena Berman on 29 Jun 2021 at 14:16
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 13 Mar 2021
rng(655321)
times = datetime('now') - minutes(sort(randi(600,10,1),'descend'))
times = 10×1 datetime array
12-Mar-2021 15:26:03 12-Mar-2021 16:13:03 12-Mar-2021 17:05:03 12-Mar-2021 17:07:03 12-Mar-2021 18:53:03 12-Mar-2021 21:06:03 12-Mar-2021 21:49:03 12-Mar-2021 21:57:03 12-Mar-2021 23:06:03 12-Mar-2021 23:28:03
knots = rand(10,1)*8
knots = 10×1
1.4359 6.3969 1.6781 3.2483 0.5765 4.2753 7.8071 1.1538 3.2355 2.5370
G = discretize(knots, [0 3 4 8], 'categorical', {'not working', 'average', 'good'})
G = 10×1 categorical array
not working good not working average not working good good not working average not working
idx = find(G(1:end-1)=="good")
idx = 3×1
2 6 7
sum(times(idx+1)-times(idx))
ans = duration
01:43:00
  5 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by