Use for loop to compare each row value out of two separate arrays to create a filtered final array

2 views (last 30 days)
I am trying to compare and filter two seperate arrays to make a final filtered array that will display a tilt perception in angles. I am trying to run it through a for loop to compare each row and spit out the final value. I was able to make it work with just two values Pitch 17.99 Roll 9.073 which spits out an angle of 25.88 but I want it to read all the row values in the arrays and give a number. Any help would be appreciated, I am stuck at this moment what to fix.
RollValue = CorrJoystickRoll1(:,1);
PitchValue = CorrJoystickPitch1(:,1);
Angle =[];
for i = 1:length(PitchValue)
if (RollValue>0)&(PitchValue>0)
Angle = atand((sind(RollValue))/tand(PitchValue));
elseif (RollValue>0)&(PitchValue<0)
Angle = 90 - atand((sind(RollValue))/tand(PitchValue));
elseif (RollValue<0)&(PitchValue<0)
Angle = 180 + atand((sind(RollValue))/tand(PitchValue));
elseif (RollValue<0)&(PitchValue>0)
Angle = 270 - atand((sind(RollValue))/tand(PitchValue));
elseif (RollValue>0)&(PitchValue==0)
Angle = 90;
elseif (RollValue<0)&(PitchValue==0)
Angle = 270;
elseif (RollValue==0)&(PitchValue>0)
Angle = 0;
elseif (RollValue==0)&(PitchValue<0)
Angle = 180;
end
Angle;
end

Accepted Answer

Voss
Voss on 18 May 2022
Edited: Voss on 18 May 2022
Inside the for loop, use RollValue(i) and PitchValue(i) instead of RollValue and PitchValue, and also you'll need to assign to Angle(i) instead of Angle each time.
However, you can do the same operations more efficiently using logical indexing rather than a for loop:

More Answers (0)

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!