Matlab: If/Elseif 2 conditions.

In summary, the conversation discusses the use of disjunctive conditions in MATLAB and how they can be used to execute code based on multiple conditions using the OR operator. It is mentioned that using an elseif statement would require repeating the same code as the if statement, and the use of logical operators is recommended for more efficient coding. A link to further resources on logical operators in MATLAB is provided.
  • #1
Beer-monster
296
0
Hi

I'm new to MATLAB and I'm trying to write a code that executes the same process under two separate conditions>

condition 1: that scalar value dE is less than or equal to zero.
condition 2: that scalar value r is less than or equal to scalar value w.

I'd like to same series of calculations to be executed if either of these conditions are true (or both) like an OR statement.

I've tried to accomplish this using an if and elseif combination.

r=0
w=1
dE=0
L1=ones(N)

for i=1:t

if (dE<=0)
elseif (r<=w)

<program statements>

else

<final condition>
end


However, it does not seem to be working. Most examples of the elseif statement I've seen online has two separate series of commands for the if and elseif condition

e.g

if expression1
<program statements1>
elseif expression2
<program statements2>
end


Is it possible that my code will not work unless I write the if/elseif statements separate (i.e. copy and paste my current process)? If so, is there another method I could use or do I need to repeat my statements.

Thanks
 
Physics news on Phys.org
  • #2
MATLAB also allows you to do disjunctive conditions. What're disjunctive conditions? That's fancy-talk for OR-ing two things (in this case, using the OR operator, the pipe |). If you're familiar with C, that probably looks pretty familiar--in fact, MATLAB uses the same operators, both logical and bit-wise.

Assume you have two numbers A and B. Let's say you want some code to execute if A is less than 5 or B is >10. To do that, you'd do the following:

Code:
if (A < 5 | B > 10)
     A * B
end

For completeness sake, it's usually best to put brackets around logical conditions in the order that you wish for them to be evaluated (say, when you have three or more operations or conditions--it's not always left to right).

Unfortunately, the only way an elseif statement would work is if you pasted the exact same code under the if statement as you do under the else-if statement. In short, use the power of logical operators:
http://www.mathworks.com/help/techdoc/matlab_prog/f2-97022.html#brftcpu-1
 
Last edited by a moderator:
  • #3
Great. That helped a lot. Thanks.
 

1. What is the syntax for using if/elseif with 2 conditions in Matlab?

The syntax for using if/elseif with 2 conditions in Matlab is as follows:
if condition1
% code to be executed if condition1 is true
elseif condition2
% code to be executed if condition2 is true
end

2. Can I use more than 2 conditions with if/elseif in Matlab?

Yes, you can use multiple conditions with if/elseif in Matlab. Simply add additional elseif statements between the initial if statement and the final else statement.

3. What happens if both conditions in if/elseif are true?

If both conditions in if/elseif are true, only the code within the first true condition will be executed. The rest of the conditions will be skipped.

4. Is it necessary to include an else statement with if/elseif in Matlab?

No, it is not necessary to include an else statement with if/elseif in Matlab. However, it is recommended as a catch-all for any conditions that are not covered by the if or elseif statements.

5. Can I nest if/elseif statements within each other in Matlab?

Yes, you can nest if/elseif statements within each other in Matlab. This can be useful for more complex conditional logic. However, it is important to keep track of the indentation and closing statements to avoid errors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
0
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
Back
Top