# Interacting with GNU Octave: Operators

0
2870

## GNU Octave Beginner’s Guide Become a proficient Octave user by learning this high-level scientific numerical tool from the ground up

The reader will benefit from the previous article on GNU Octave Variables.

## Basic arithmetic

Octave offers easy ways to perform different arithmetic operations. This ranges from simple addition and multiplication to very complicated linear algebra. In this section, we will go through the most basic arithmetic operations, such as addition, subtraction, multiplication, and left and right division. In general, we should think of these operations in the framework of linear algebra and not in terms of arithmetic of simple scalars. ## Time for action – doing addition and subtraction operations

1. I have lost track of the variables! Let us start afresh and clear all variables first:

octave:66> clear

(Check with whos to see if we cleared everything).

2. Now, we define four variables in a single command line(!)

octave:67> a = 2; b=[1 2 3]; c=[1; 2; 3]; A=[1 2 3; 4 5 6];

Note that there is an important difference between the variables b and c; namely, b is a row vector, whereas c is a column vector.

3. Let us jump into it and try to add the different variables. This is done using the + character:

octave:68>a+a

ans = 4

octave:69>a+b

ans =
3 4 5

octave:70>b+b

ans =
2 4 6

octave:71>b+c

error: operator +: nonconformant arguments (op1 is 1×3, op2 is
3×1)

It is often convenient to enter multiple commands on the same line. Try to test the difference in separating the commands with commas and semicolons.

### What just happened?

The output from Command 68 should be clear; we add the scalar a with itself. In Command 69, we see that the + operator simply adds the scalar a to each element in the b row vector. This is named element-wise addition. It also works if we add a scalar to a matrix or a higher dimensional array.

Now, if + is applied between two vectors, it will add the elements together element-wise if and only if the two vectors have the same size, that is, they have same number of rows or columns. This is also what we would expect from basic linear algebra.

From Command 70 and 71, we see that b+b is valid, but b+c is not, because b is a row vector and c is a column vector—they do not have the same size. In the last case, Octave produces an error message stating the problem. This would also be a problem if we tried to add, say b with A:

octave:72>b+A

error: operator +: nonconformant arguments (op1 is 1×3, op2 is 2×3)

From the above examples, we see that adding a scalar to a vector or a matrix is a special case. It is allowed even though the dimensions do not match! When adding and subtracting vectors and matrices, the sizes must be the same. Not surprisingly, subtraction is done using the – operator. The same rules apply here, for example:

octave:73> b-b

ans =
0 0 0

is fine, but:

octave:74> b-c

error: operator -: nonconformant arguments (op1 is 1×3, op2 is 2×3)

produces an error.

### Matrix multiplication

The * operator is used for matrix multiplication. Recall from linear algebra that we cannot multiply any two matrices. Furthermore, matrix multiplication is not commutative. For example, consider the two matrices: The matrix product AB is defined, but BA is not. If A is size n x k and B has size k x m, the matrix product AB will be a matrix with size n x m. From this, we know that the number of columns of the “left” matrix must match the number of rows of the “right” matrix. We may think of this as (n x k)(k x m) = n x m. In the example above, the matrix product AB therefore results in a 2 x 3 matrix: ## Time for action – doing multiplication operations

Let us try to perform some of the same operations for multiplication as we did for addition:

octave:75> a*a

ans = 4

octave:76> a*b
ans =
2 4 6

octave:77> b*b

error: operator *: nonconformant arguments (op1 is 1×3, op2 is 1×3)

octave:78> b*c

ans = 14

### What just happened?

From Command 75, we see that * multiplies two scalar variables just like standard multiplication. In agreement with linear algebra, we can also multiply a scalar by each element in a vector as shown by the output from Command 76. Command 77 produces an error—recall that b is a row vector which Octave also interprets as a 1 x 3 matrix, so we try to perform the matrix multiplication (1 x 3)(1 x 3), which is not valid. In Command 78, on the other hand, we have (1 x 3)(3 x 1) since c is a column vector yielding a matrix with size 1 x 1, that is, a scalar. This is, of course, just the dot product between b and c.

Let us try an additional example and perform the matrix multiplication between A and B discussed above. First, we need to instantiate the two matrices, and then we multiply them:

octave:79> A=[1 2; 3 4]; B=[1 2 3; 4 5 6];

octave:80> A*B

ans =
9 12 15
19 26 33

octave:81> B*A

error: operator *: nonconformant arguments (op1 is 2×3, op2 is 2×2)

Seems like Octave knows linear algebra!

### Element-by-element, power, and transpose operations

If the sizes of two arrays are the same, Octave provides a convenient way to multiply the elements element-wise. For example, for B:

octave:82> B.*B

ans =
1 4 9
16 25 36

Notice that the period (full stop) character precedes the multiplication operator. The period character can also be used in connection with other operators. For example:

octave:83> B.+B

ans =
2 4 6
8 10 12

which is the same as the command B+B.

If we wish to raise each element in B to the power 2.1, we use the element-wise power operator.ˆ:

octave:84> B.^2.1

ans =
1.0000 4.2871 10.0451
18.3792 29.3655 43.0643

You can perform element-wise power operation on two matrices as well (if they are of the same size, of course):

octave:85> B.^B

ans =
1 4 27
256 3125 46656

If the power is a real number, you can use ˆ instead of .ˆ; that is, instead of Command 84 above, you can use:
octave:84>Bˆ2.1

Transposing a vector or matrix is done via the ‘operator. To transpose B, we simply type:

octave:86> B’

ans =
1 4
2 5
3 6

Strictly, the ‘ operator is a complex conjugate transpose operator. We can see this in the following examples:

octave:87> B = [1 2; 3 4] + I.*eye(2)

B =
1 + 1i 2 + 0i
3 + 0i 4 + 1i

octave:88> B’

ans =
1 – 1i 3 – 0i
2 – 0i 4 – 1i

Note that in Command 87, we have used the .* operator to multiply the imaginary unit with all the elements in the diagonal matrix produced by eye(2). Finally, note that the command transpose(B)or the operator .’ will transpose the matrix, but not complex conjugate the elements.