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