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 个评论
Robert Munoz
Robert Munoz 2020-10-24
The loop is supposed to go until the last voltage is assigned one of the letter values
Walter Roberson
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
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by