Octave Graphical Output
This entry is part 4 of 11 in the series GNU Octave

Expressions are the basic building block of statements in Octave. It can be created using values, variables that have already been created, operators, functions, and parentheses. An expression evaluates to value and can serve as a statement on its own. Generally, statements contain one or more expressions that specify data to be operated on. Like the earlier articles, we encourage readers planning to learn octave/Matlab to try the exercises marked as “Ex“.

Assignment Expressions

As the name suggests an assignment expression is used to store a value in a variable. The most common assignment operator is “=”, which evaluates the value of an expression on right and stores the output in a variable on left. Assignment operator can be used with other mathematical operators also to shorten the expression e.g. “+=”, “-=”, “*=” e.t.c. In such a case, the operation is performed on the initial left-hand side variable and right-hand side expression. And the final value is updated in the left-hand-side variable.

>> var = 2
var = 2
>> var += 3
var = 5

Operators

Like any programming language, two kinds of operators can be possible: unary operators, which operate on a single value or operand; and binary operators, which operate on two values or operands. A “-” operator is both unary (negation) as well as binary (minus) in nature. In this article, we will discuss basic operator use only. As the series progresses we will introduce more advanced use cases.

Increment Operators

Adding to a variable is called incrementing. But in general, both increment and decrement are defined as addition or subtraction of 1 from the variable. It is denoted as “++” or “–” symbol. Increment operators can be used both before or after the variable. In both cases, the variable is incremented or decremented. But the output of expression will be updated value if an operator is used before variable, else previous value will be returned.

>> ++var
var = 6
>> var++
var = 6
>>var
var = 7

Arithmetic Operators

Some of the common operators that can be used with numeric expressions:

  • +” addition
  • ” negation, minus
  • *” multiplication
  • /” division
  • \” left division (x\y is equivalent to y/x)
  • ^” or “*” exponent

When the data used is matrix or vector all the above operators can also be used with a dot before the operator to denote element-wise operation e.g. “x*y” is normal multiplication while “x. *y” is element-wise multiplication. In this case, two more operators are introduced. The use of these operators is discussed in “Octave:1>Matrix and Vector Operations”.

  • ” complex conjugate transpose
  • .’” transpose
>> a=[1,2,3]
a =
   1   2   3
>> a*a
error: operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3)
>> a.*a
ans =
   1   4   9
Ex: Try and use arithmatic operators as increment operators, and analyze the outcomes.

Comparison Operators

These are the relation operators that return the logical type value after evaluation of an expression.

  • <” true if LHS is less then RHS
  • <=” true if LHS is less then or equal to RHS
  • >” true if LHS is greater then RHS
  • >=” true if LHS is greater then or equal to RHS
  • ==” true if LHS is equal to the RHS
  • !=” or “~=” true if LHS is not equal to the RHS

All of Octave’s comparison operators return a value of 1 if the comparison is true, or 0 if it is false. In the case of a matrix or vector, these operators work on an element-by-element basis.

Boolean Operators

A boolean expression can be divided into two types when dealing with octave. The first one is element-wise boolean, in this case, if a value is 0 then it is considered false else true.

  • &” element wise AND e.g. 2 & 3 = 1(true).
  • |” element-wise OR
  • !” or “~” element-wise NOT

On the other hand, the second type of boolean operators is called short circuit operators. these only operate on logical type operands.

  • &&” logical AND
  • ||” logical OR

Selfhelp

  • format” command changes the output precision. The default value is short, i.e. 4 digits after the decimal point.
>> pi
ans =  3.1416
>> format long
>> pi
ans =  3.14159265358979
>> format short
>> pi
ans =  3.1416
  • ” three dots (ellipsis) are used to continue the expression in the next line as a continuation. this is very helpful for the readability of long expressions.
>> 2 + 3 + 5 - 8 ...
* 2 / 9
ans =  8.2222
  • help” command can elaborate more about the operators e.g. “help +“.
Ex: Try to write multiple expressions using operators described in this article.

This article should serve as the introduction to operators and basic expressions in GNU Octave. We will visit these back with advanced use cases at multiple stages in the ongoing series. Please write down any queries/suggestions related to the article as a comment.

Octave development takes a lot of time and expertise. Your contributions help to ensure that Octave will continue to improve.

Operator Precedence

Operator precedence determines how operators are grouped when different operators appear close by in one expression.

>> 2 + 3 * 2 + 4
ans = 12

In the above expression, if we go left to right solving operators, the output should be 14. But if we go from right to left, the output should be 20. Although the actual answer is different from both indicating that one of the operators got preference over another. In the above example, it is clear that ‘*’ has higher precedence than ‘+’. Operators are mentioned below in order of decreasing precedence. Unless noted, all operators group left to right.

  • Function call and array indexing, cell array indexing, and structure element indexing.
‘()’ ‘{}’ ‘.’
  • Postfix increment, and postfix decrement.
‘++’ ‘--’
These operators group right to left.
  • Transpose and exponentiation.
‘'’ ‘.'’ ‘^’ ‘’ ‘.^’ ‘.’
  • Unary plus, unary minus, prefix increment, prefix decrement, and logical “not”
‘+’ ‘-’ ‘++’ ‘--’ ‘~’ ‘!’
  • Multiply and divide
‘*’ ‘/’ ‘\’ ‘.\’ ‘.’ ‘./’
  • Add, subtract
‘+’ ‘-’
  • Colon (Index expression allows to reference or extract selected elements of a matrix or vector)
‘:’
  • Relational
‘<’ ‘<=’ ‘==’ ‘>=’ ‘>’ ‘!=’ ‘~=’
  • Element-wise “and”
‘&’
  • Element-wise “or”
‘|’
  • Logical “and”
‘&&’
  • Logical “or”
‘||’
  • Assignment
‘=’ ‘+=’ ‘-=’ ‘=’ ‘/=’ ‘=’ ‘^=’ ‘.=’ ‘./=’ ‘.=’ ‘.^=’ ‘|=’ ‘&=’
These operators group right to left.

Reference: https://octave.org/doc/v4.2.1/Operator-Precedence.html#Operator-Precedence

Ex: With help of multiple expressions try to verify the predesence rule.

Use this article as a reference while formatting an expression. If the precedecence rule is not kept in mind then it might be possible to get the result which is not desired at all. Please write down any queries/suggestions related to the article as a comment.

By Purnendu Kumar

Purnendu is currently working as Senior Project Engineer at QuNu Labs, Indias only Quantum security company. He has submitted his thesis for Masters (MS by research 2014-17) in electrical engineering at IIT Madras for doing his research on “constant fraction discriminator” and “amplitude and rise-time compensated discriminator” for precise time stamping of Resistive Plate Chamber detector signals. In collaboration with India Based Neutrino observatory project, he has participated in design and upgrade of FPGA-based data acquisition system, test-jig development, and discrete front-end design. After completion of his bachelors in Electrical Engineering (Power and Electronics), he was awarded Junior Research Fellowship at Department of Physics and Astro-physics, University of Delhi under same (INO) project. His current interest is in high-speed circuit design, embedded systems, IoT, FPGA implementation and optimization of complex algorithms, experimental high-energy physics, and quantum mechanics.