Matlab: If/Elseif 2 conditions.

  • Context: MATLAB 
  • Thread starter Thread starter Beer-monster
  • Start date Start date
  • Tags Tags
    Conditions Matlab
Click For Summary
SUMMARY

This discussion focuses on implementing conditional statements in MATLAB, specifically using the OR operator to execute code under multiple conditions. The user seeks to execute the same series of calculations if either the scalar value dE is less than or equal to zero or if r is less than or equal to w. The solution involves using the logical OR operator (|) in a single if statement rather than separate if and elseif statements, which is a more efficient approach in MATLAB programming.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with conditional statements in programming
  • Knowledge of logical operators in MATLAB
  • Experience with scalar variables in MATLAB
NEXT STEPS
  • Learn about MATLAB logical operators, specifically the OR operator (|)
  • Explore MATLAB's conditional statements and their syntax
  • Investigate best practices for writing efficient MATLAB code
  • Review MATLAB documentation on control flow and logical conditions
USEFUL FOR

MATLAB programmers, data analysts, and anyone looking to optimize their use of conditional statements in MATLAB for efficient coding practices.

Beer-monster
Messages
285
Reaction score
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
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:
Great. That helped a lot. Thanks.
 

Similar threads

Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K