Combination of "for loop" and "if statement"
3 views (last 30 days)
Show older comments
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
Stephen23
on 17 Jun 2021
Edited: Stephen23
on 17 Jun 2021
Original question by Cynthia Lee on 16th of March 2021, retrieved from Bing Cache:
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
Answers (1)
Walter Roberson
on 13 Mar 2021
rng(655321)
times = datetime('now') - minutes(sort(randi(600,10,1),'descend'))
knots = rand(10,1)*8
G = discretize(knots, [0 3 4 8], 'categorical', {'not working', 'average', 'good'})
idx = find(G(1:end-1)=="good")
sum(times(idx+1)-times(idx))
5 Comments
Walter Roberson
on 13 Mar 2021
times = readmatrix('NameOfTimeFile.xlsx');
knots = readmatrix('NameOfKnotsFile.xlsx');
G = discretize(knots, [0 3 5 15], 'categorical', {'not working', 'average', 'good'});
average_times = nnz(G=="average");
good_times = nnz(G=="good");
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!