if statement gives wrong results or does not work?
3 views (last 30 days)
I've got the following if statement within a for loop. In the beggining, i thought it's quite straight forward. I've got a scalar and an array, I have to compare each value of the array with that scalar and after that, to make certain calculations.
a_sat = -1.2;
a = [-5 -2 -7 0 -1 3 5];
for i = 1:length(a)
if a(1:length(a) ) >=a_sat
b = 0.45;
else if a(1:length(a)) < a_sat
b(i) = 3.*a;
This gives an undefined value of b. If I try to remove the counter in the if's, then I get a wrong result for the value of b. can anyone please help me with that? Thank you!
Iain on 15 Jul 2013
"any" and "all" are your friends,
You have actually written
if ALL of a >= a_sat
else, if ALL of a < sat
else % (i.e. some of a > sat and some < sat)
I don't know what you're actually wanting.
I suspect what you want is something like this
if a(i) > a_sat % if just the "ith" part of a is > a_sat
b = 0.45; % set b to "max"
else %if not...
b = 3*a(i); % b = 3* the "ith" part of a.