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

2 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-5-18
编辑:Voss 2022-5-18
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by