Preliminaries and Data types

  • GNU Octave

Declaration of scalar data

In [1]:
a = 2;
b = -1;
c = 4;
d = 10.0;
  • Some elementary arithmetic operations using the above defined scalars:
In [2]:
e = a+b;
f = a-b;
g = a*b*c;
h = a/d;
  • Printing out various variables can be done as below. In what follows we have the following placeholders
    • %d: A placeholder for an integer
    • %f: A placeholder for a floating point number
    • \t: Inserts a tab in the printing
    • \n: Inserts a newline in the printing We will encounter other such placeholders in due course.
  • The placeholders, tab, and newline characters can be used to achieve a human readable output quite easily.
In [3]:
fprintf("The value of e is: %d\t", e);  
fprintf("where a = %d\t", a); 
fprintf("and b = %d\n", b);
The value of e is: 1	where a = 2	and b = -1
  • Another way of printing out variables on the terminal (or in a cell in this particular case) is simply writing the name of the variable and skipping the semicolon.
    • A semicolon helps in suppressing the output on the terminal.
    • The function $\textrm{fprintf}$ can, however, be used in a more versatile manner.
    • Apart from printing to the terminal as shown above, it can be used to output to a file as well.

Declaration of an array

  • An array can be declared manually as shown below:
In [4]:
a=[5.001,10.00,11.00,-2.00,6.00]
a =

    5.0010   10.0000   11.0000   -2.0000    6.0000

  • We can query the size of the array using $\textbf{size}$.
  • This can be useful when the arrays are arbitrarily long and you want to know how long that particular array is. For example, the data could be a time series of velocity in a flow field as measured with the help of an anemometer.
In [5]:
size(a) # The array is 1 row and 5 columns
ans =

   1   5

  • An array can be initialized with zero values using $\textbf{zeros}$. It creates a zero array with 1 row and 5 columns.
In [6]:
b = zeros (1,5) # Notice how the lack of a semicolon results in printing out the statement below:
b =

   0   0   0   0   0

We can create a linear space (arithmetic progression) from point a to b with the help of the function $\textbf{linspace}$.

In [7]:
c = linspace(1, 2, 5) # Here, the linspace will go from 1 to 2 and will create 5 steps, inclusive of 1 and 2.
c =

   1.0000   1.2500   1.5000   1.7500   2.0000

  • We can also provide the step size and infer the number of elements using $\textbf{colon(:)}$ operator
In [8]:
c = 1:0.25:2
c =

    1.0000    1.2500    1.5000    1.7500    2.0000

  • We can similarly create a logspace. The fundamental difference between the logspace and linspace is that the former is a geometric progression while the other is an arithmetic progression.
  • The function call to logspace looks quite similar to linspace but whatever linspace would have produced, is the exponent of 10. Thus creating essentially, $10^{\textrm{equivalent linspace}}$
In [9]:
d = logspace(1, 4, 4)
d =

      10     100    1000   10000

  • In the above expression; we basically have; 10^1, 10^2, 10^3, 10^4

Some elementary array operations:

  • Let us create another array $\textrm{e}$ and printout the two arrays $\textrm{a}$ and $\textrm{e}$.
In [10]:
e=[-2.18568893  0.74171085  0.95996753  1.13442837  1.62010761];
printf("%f \t", a); printf("\n"); printf("%f \t", e);
5.001000 	10.000000 	11.000000 	-2.000000 	6.000000 	
-2.185689 	0.741711 	0.959968 	1.134428 	1.620108 	
  • Let us perform element wise addition; save the output in the variable $\textrm{b}$, and then printout the elements of $\textrm{b}$.
In [11]:
b = a+e; printf("%f", b); # Element-wise addition
2.81531110.74171111.959968-0.8655727.620108
  • Similarly we can perform other elementwise operations such as subtraction, multiplication, and division.
  • A note of warning
    • * and .* are not the same things.
    • * implies matrix like multiplication while .* implies element-wise multiplication
    • Similarly / and ./ stand for matrix division and element-wise division respectively.
In [12]:
c = a-e;
d = a.*e;
f = a./e;
disp(c); # The function disp can also be used as a no-frills variable display on the command line
disp(d);
disp(f);
    7.1867    9.2583   10.0400   -3.1344    4.3799
  -10.9306    7.4171   10.5596   -2.2689    9.7206
   -2.2881   13.4823   11.4587   -1.7630    3.7035
  • We can multiply a scalar with an array
In [13]:
c = 2*a;
disp(a);
disp(c);
    5.0010   10.0000   11.0000   -2.0000    6.0000
   10.0020   20.0000   22.0000   -4.0000   12.0000
  • We can perform element-wise exponenetiation using $\textbf{.^}$ operator. Let's create a linearly spaced array from $1$ to $4$ and a log spaced array from $10$ to $10^4$
In [14]:
c = linspace(1., 4., 4);
disp(c)
d = logspace(1., 4., 4);
disp(d)
e = 10.^c;
disp(e)
   1   2   3   4
      10     100    1000   10000
      10     100    1000   10000

Splicing an array.

  • Let use the previous array $\textrm{f}$ and select the $3$rd through $5$th element of the array
In [15]:
disp(f);
   -2.2881   13.4823   11.4587   -1.7630    3.7035
In [16]:
f(3:5)
ans =

   11.4587   -1.7630    3.7035

  • This is array splicing; it will splice till the n_th index
  • In GNU Octave, the array indexing starts from 1 and ends at n; where n is the number of elements in that array.
  • In Python, array indexing starts from 0 and ends at n-1; where n is the number of elements in that array.

Creation of a random array can be done as following:

In [17]:
h = rand([1,10]) # Random array of size 1x10 with a uniform distribution between 0 and 1
h =

 Columns 1 through 8:

   0.88492   0.37098   0.66118   0.37255   0.26541   0.39928   0.28560   0.21967

 Columns 9 and 10:

   0.79745   0.35710

  • The array can be spliced as follows and assigned to a new variable $\textrm{'k'}$
In [18]:
k = h(2:8)
k =

   0.37098   0.66118   0.37255   0.26541   0.39928   0.28560   0.21967

  • We can make use of the special keyword $\textbf{end}$ to reference the last element of an array.

  • The statement below shows how to print multiple entries on the same line as well.

In [19]:
fprintf("First element: %f \t Last element: %f\n", k(1), k(end))
First element: 0.370983 	 Last element: 0.219674

Some conditionals on arrays

In [20]:
fprintf("%f ", k); fprintf("\n");
m = k>0.5; # This line checks whether the entries of array k are larger than 0.5. If yes, then that particular index of m gets a value of 1 (boolean for True) else m gets a value of zero
fprintf("%d ", m);
0.370983 0.661180 0.372547 0.265415 0.399284 0.285598 0.219674 
0 1 0 0 0 0 0 
In [21]:
m = k<0 # For logical false Octave gives 0 as output
m =

  0  0  0  0  0  0  0

Declaration of a matrix

  • A matrix can be initialized with zeros as shown:
In [22]:
a=zeros(4,4)
a =

   0   0   0   0
   0   0   0   0
   0   0   0   0
   0   0   0   0

  • A random matrix can be declared as follows (uniform random values between 0 and 1)
In [23]:
b = rand(4,4); disp(b); #Random number generation
   0.8044254   0.1757793   0.2477413   0.6133617
   0.5444011   0.9068164   0.7261868   0.1630314
   0.0050245   0.4159495   0.1479635   0.2794364
   0.3144671   0.6422521   0.4563205   0.7087710
  • A matrix can be initialized with all elements as $1$ as shwon:
In [24]:
e = ones(4,4)
e =

   1   1   1   1
   1   1   1   1
   1   1   1   1
   1   1   1   1

  • Scalar multiplication of matrix
In [25]:
g = 2*b #Scalar multiplication
g =

   1.608851   0.351559   0.495483   1.226723
   1.088802   1.813633   1.452374   0.326063
   0.010049   0.831899   0.295927   0.558873
   0.628934   1.284504   0.912641   1.417542

  • Scalar multiplication of matrix
In [26]:
h=g*b #Proper matrix multiplication
h =

   1.87384   1.59556   1.28697   2.05204
   1.97304   2.64955   1.95047   1.60046
   0.63820   1.23817   0.90542   0.62060
   1.65557   2.56540   1.87049   1.85492

  • element-wise multiplication
In [27]:
k=g.*b #Element wise multiplication
k =

   1.294200376   0.061796717   0.122751536   0.752425095
   0.592745150   1.644632125   1.054694513   0.053158502
   0.000050492   0.346028030   0.043786423   0.156169426
   0.197779132   0.824975547   0.416456882   1.004712624

In [28]:
p = k>1
p =

  1  0  0  0
  0  1  1  0
  0  0  0  0
  0  0  0  1

In [29]:
k(k>1)
ans =

   1.2942
   1.6446
   1.0547
   1.0047

In [ ]: