Im supposed to make a function that names fruit based on the voltages using a while loop. The function is supposed to take the voltages and assign them to a variable based off of a set range of voltages for the variable. This is the voltage matrix we are given volt = [18 33 31 34 15 37 10.5 48 50 38 35 39 42 33 31 1 5 9 13 11 27 35 -1 46 22 6 19 36] and we are supposed to sort them as follows; 0 and 10 volts=A , 11 and 20=P, 21 and 30=G, 31 and 40=O, and < 0 or > 40=U
This is my function bellow. I don't really know what I'm doing since I'm not really afluent in coding and my professor is pretty garbage so any help would be nice. Thank you
function [f] = While_Loop(volt)
fruit=volt;
while true
if volt <=10 & volt>=0
fruit=A;
elseif volt>=11 & volt<=20
fruit=P;
elseif volt>=21 & volt<=30
fruit=G;
elseif volt>=31 & volt<=40
fruit=O;
elseif volt<=0 & volt>=40
fruit=U;
end
break;
end
fruit=[];
f=fruit;
end

댓글 수: 3

What is the termination condition for the while loop? For example are you expected to terminate when you reach -1 ? Or is the while loop just being arbitrarily required to teach you about while loops to substitute for more natural for loops?
Robert Munoz
Robert Munoz 2020년 10월 24일
The loop is supposed to go until the last voltage is assigned one of the letter values
So a "make-work" while loop then, leading to worse code than a for loop. Oh well.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 24일

0 개 추천

Hint:
initialize output to empty
while the input is not empty
remove the first value from the input and record it, chopping it out of the input vector
take the recorded input and use if statements to classify it
append the result of the classification to the end of the output matrix
end of while loop
At the end of this, your input will be empty and your output will have one entry for each input value.

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2020년 10월 24일

댓글:

2020년 10월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by