Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ternary operator in matlab

is there a way of typing for if like:

var = (cond) ? true : false; 

or do we have to use this format?

if (cond)  true else  false end 
like image 436
Gün Karagöz Avatar asked Apr 08 '11 12:04

Gün Karagöz


People also ask

Does Matlab have a ternary operator?

MATLAB does not have the ternary operator, so you have to use the if statement. The ternary operator in some languages is used to define the else statement.

What is a ternary operator?

The ternary operator is an operator that exists in some programming languages, which takes three operands rather than the typical one or two that most operators use. It provides a way to shorten a simple if else block.

What is ternary operator with example?

Example: C Ternary Operator Here, age >= 18 - test condition that checks if input value is greater or equal to 18. printf("You can vote") - expression1 that is executed if condition is true. printf("You cannot vote") - expression2 that is executed if condition is false.

How ternary operator is represented?

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.


1 Answers

MatLab doesn't have a ternary operator, or any other syntactic sugar for one-line if-statements. But if your if-statement is really simple, you could just write it in one line anyway:

if (cond); casetrue(); else; casefalse(); end 

It's not as simple as ternary operator, but still better than writing it in 5 lines of code.

like image 160
Leonid Beschastny Avatar answered Oct 07 '22 17:10

Leonid Beschastny