+ or - Bias Based on < or >

I am attempting to add or subtract a bias value based on weither variable AccelLateral_PT is less or greater than AccelLat. The resulting Corrected_Lateral_Accel for the code below is only resulting in the addition of the bias value. Could anyone help? Thank you.
load('Lateral_Accel_Data')
if AccelLateral_PT > AccelLateral
Corrected_Lateral_Accel = AccelLateral_PT - Lateral_Accel_Bias;
else
Corrected_Lateral_Accel = AccelLateral_PT + Lateral_Accel_Bias;
end

Answers (1)

Steven Lord
Steven Lord on 10 Dec 2020

0 votes

I suspect at least one of AccelLateral_PT and AccelLateral is not scalar. From the documentation page for the if keyword: "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." Because of this, you will only subtract the bias if every element of AccelLateral_PT is greater than AccelLateral. If even one element is not, the else block is executed.
If my suspicion is correct, you want to use logical indexing instead.

Categories

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

Asked:

on 10 Dec 2020

Answered:

on 10 Dec 2020

Community Treasure Hunt

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

Start Hunting!