MATLAB Answers

excel if statement to matlab

4 views (last 30 days)
hi
so i know there are many efieciant ways to do things on matlab without the need to use the if statement, and I was wondering if someone could help out on converting this excel formula/code to matlab without the need for a double if statements
=IF(AND(E5=0,E4<>0),F4+1,F4)

Accepted Answer

Monika Jaskolka
Monika Jaskolka on 30 Jul 2021
Edited: Monika Jaskolka on 30 Jul 2021
I don't see that a "double if" is necessary:
x = 0;
if (~E5 && E4)
x = F4 + 1;
else
x = F4;
end
If you are asking for a one line conditional assignment like below, Matlab doesn't support this syntax
condition ? true-expression : false-expression
  6 Comments
Rik
Rik on 2 Aug 2021
@Monika Jaskolka Consider teaching yourself to use numel instead of length. It is never a worse option, and it might save you from a difficult bug hiding in your program due to array input (instead of vector inputs).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!