I have a loop that evaluates multiple 'if' statements, however the 'if' statements are incorrectly evaluating logical <, >, and == expressions.
I would expect two variables with a value of 0.1, to be considered equivalent == but sometimes the looped 'if' statements records that one variable is larger than the other. The loop is the last portion of the code included in this message. This may be a rounding error ….I may need to inform the compiler that any variables that differ by less than 0.000001 should be considered equivalent… Any help would be greatly appreciated.
This code will compare RangeRef = 0.1, to each element of the array Ranges = [0.1, 0.1, 0.08, 0.02]
Correct operation would count the number of array elements that are less than RangeRef, equal to, and greater than…correct results in OneGroup1 = 2, OneGroup2 = 2, OneGroup3 = 0
I am getting OneGroup1 = 2, OneGroup2 = 1, OneGroup3 = 1
Here is the code: %-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
upper=[0.9, 0.15, 0.1, 0.05];
lower=[0.8, 0.05, 0.02, 0.03];
Ranges = [1:4];
%Calculates Ranges
TempQ = 4;
while TempQ > 0
Ranges(TempQ) = upper(TempQ) - lower(TempQ); TempQ = TempQ - 1;
end
%Calculates Ru, Rl, and Rp
Temp1Q = SLComponents;
SumUpper = 0;
SumLower = 0;
while Temp1Q > 0
SumUpper = SumUpper + upper(Temp1Q); SumLower = SumLower + lower(Temp1Q); Temp1Q = Temp1Q - 1;
end
RangeUpper = SumUpper – 1;
RangeLower = 1 – SumLower;
RangeRefArray = [RangeUpper, RangeLower];
RangeRef = min(RangeRefArray)
TempG1 = SLComponents;
OneGroup1 = 0; OneGroup2 = 0; OneGroup3 = 0;
while TempG1 > 0
if Ranges(TempG1) < RangeRef OneGroup1 = OneGroup1 + 1; elseif Ranges(TempG1) > RangeRef OneGroup3 = OneGroup3 + 1; elseif Ranges(TempG1) == RangeRef OneGroup2 = OneGroup2 + 1; end TempG1 = TempG1 - 1;
end
Best Answer