How do I assign names to values in a matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2020-10-24
So a "make-work" while loop then, leading to worse code than a for loop. Oh well.
回答(1 个)
Walter Roberson
2020-10-24
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!