Need Help TroubleShooting If/Else Statement

Helo,
Basically I have an if/else statement to ensure a given value obtain from cosine and sine is between -180 and +180.
In other words, if the value ends up being 270-deg, I want it to be -90-deg instead.
I set up an if/else statement as follows, where phi2a comes from a value from a function.
phi2b = 180-phi2a;
if phi2b > 180
phi2b = phi2b-360;
else
phi2b;
end
When I do this, however, whenever phi2b is greater than 180, it still produces a value greater than 180.
I tried to do a sample code (below) to see if the syntax is correct, but this one ended up working when I change the value of A:
A = 1;
if A > 10
A = A+5;
else
A;
end
So if A=1, then it outputs 1. If A = 20, it outputs 25. This is how I want the other if/else to function.
I can show more of my code if need be.
Basically the phi2a is a vector running from 1 to N, where N is 4393. I then plot either phi1a, phi1b, phi2a, or phi2b (Not all of which is shown here), based on another if/else statement. I can show this code as well if it'll help.
If this is confusing please let me know so I can add more information.
Any help is appreciated. Thanks.
Edit: I've added a snippet of my whole code in case that helps.

 Accepted Answer

Is it possible that your input value was greater than 540? Because if it was then it will still be greater than 180 after your if statement:
phi2b = 550;
if phi2b > 180
phi2b = phi2b-360
end
phi2b = 190
You could take care of this case by using a while loop instead:
phi2b = 550;
while phi2b > 180
phi2b = phi2b-360;
end
disp(phi2b)
-170

7 Comments

No, that is not the case. I know the first instance where phi2b > 180 is the 695th point, where phi2b = 180.2102, and looking in my workspace, the highest value it has is about 266.
Thanks though.
Are you able to upload a self-contained code snippet that will allow us to reproduce the behavior? (You can use the paperclip icon in the toolbar to do that.)
Sure, I just added it, thank you
Ah ... I see the problem.
The if statement is not actiing on the vector in the way you expect. The statement
if phi2b > 0
only evaluates as true if it is true for all elements of the vector.
Instead, I would use the following:
idx = phi2b > 180;
phi2b(idx) = phi2b(idx) - 180;
Just so I understand what you wrote, the idx is just a variable that equals phi2b only if it is greater than 180, correct?
So then you say if it is greater than 180, then phi2b at that particular spot is that value minus 180?
idx is a logical variable, the same length as phi2b, that is "true" when phi2b is greater than 180, and "false" when it is not:
phi2b = [179 180 181 182];
idx = phi2b > 180
idx = 1×4 logical array
0 0 1 1
The next line of my code then uses logical indexing to subtract 180 from the elements where idx = "true":
phi2b(idx) = phi2b(idx) - 180
phi2b = 1×4
179 180 1 2
Okay thank you for your help. That makes sense why my original method didn't work

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!