How do I assign names to values in a matrix

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 comentarios

Walter Roberson
Walter Roberson el 24 de Oct. de 2020
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 el 24 de Oct. de 2020
The loop is supposed to go until the last voltage is assigned one of the letter values
Walter Roberson
Walter Roberson el 24 de Oct. de 2020
So a "make-work" while loop then, leading to worse code than a for loop. Oh well.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Oct. de 2020

0 votos

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.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Oct. de 2020

Comentada:

el 24 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by