I need help with for loops with if statements. I'm trying to get it to output [P,O,O,O,P​​,O,P,U,U,​O​,O,O,U,O​,O​,A,A,A,​P,P​,G,O,U​,U,G​,A,P,​O]. thx

1 view (last 30 days)
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];
[fruit_for]=Program05a(volt);
% separate scirpt below
function[fruit_for]=Program05a(volt)
fruit_for = [];
for X=1:length(volt)
if volt(X)>=31 && volt(X)<=40
fruit_for= [fruit_for 'O']
elseif volt(X)>=21 && volt(X)<=30
fruit_for=[fruit_for 'G']
elseif volt(X)>=11 && volt(X)<=20
fruit_for=[fruit_for 'P' ]
elseif volt(X)>=0 && volt(X)<=10
fruit_for=[fruit_for 'A']
elseif volt(X)<0 || 40<volt(X)
fruit_for=[fruit_for 'U']
end
end

Accepted Answer

Tyler Vincent
Tyler Vincent on 12 Mar 2019
I got it to work using the updated script shown above.
  2 Comments
Guillaume
Guillaume on 12 Mar 2019
Edited: Guillaume on 12 Mar 2019
I would strongly recommed that you use (and understand) Andrei's solution instead of your own, which is very inefficient and very fragile.
In particular,
  • you grow the fruit_for array in a loop. Your array should be preallocated otherwise, it's going to make your code very slow for even moderately large inputs.
  • your code only works with a limited range of integers, but never check that the inputs are integer and within that range. It will return incorrect result with no warning that anything is wrong if that's no the case. There's probably nothing worse than code that appear to work but returns incorrect results.
  • your code only works with vector inputs, will return a row vector even if the input is a column vector. If the input is a matrix, it will return partially incorrect results without otherwise informing the user.
  • your solution is not scalable. What if you need 100 different threshold? How about 1000? Are you going to be typing 1000 elseif? Using discretize or histcounts as in Andrei's answer, you simply have to pass one array of thresholds.
Tyler Vincent
Tyler Vincent on 12 Mar 2019
Edited: Tyler Vincent on 12 Mar 2019
Hi Guillaume,
I had to use for loops with if statements for my class as it was a requirement of the assigment. I agree Andrei's work is much better and effcient than the method i'm using but for this class I had to do it in a manner like this. The range was a set amount of number's shown above for this assigment. Also i've done some revisions not shown in the script above.

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 11 Mar 2019
Edited: Andrei Bobrov on 11 Mar 2019
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];
ABS = sprintfc('%c','UAPGOU');
[~,~,ii] = histcounts(volt,[-inf,0:10:40,inf]);
fruit = ABS(ii);
or
fruit = discretize(volt,[-inf,0:10:40,inf], 'categorical', cellstr(('UAPGOU')'));

Walter Roberson
Walter Roberson on 11 Mar 2019
if 31 <= volt(X) && volt(X) <= 40
fruit = O;
elseif 21 <= volt(X) && volt(X) <= 30
fruit = G;
and so on
end
However, you have not defined O, G, P, A, or U within the function. Also, if the input were 30.5 then is it really true that you would want U? And if the input were 44 is it really the case that you want the function to crash because fruit would not have been assigned to?
  5 Comments
Walter Roberson
Walter Roberson on 12 Mar 2019
That function is from the Communications Systems Toolbox.
Use quantize() with the list of edges, and get back the corresponding index number. You can then use that to look up the table of corresponding characters. Or you could use categoricals to represent the values.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!