a = 2;
b = -1;
c = 4;
d = 10.0;
e = a+b;
f = a-b;
g = a*b*c;
h = a/d;
fprintf("The value of e is: %d\t", e);
fprintf("where a = %d\t", a);
fprintf("and b = %d\n", b);
a=[5.001,10.00,11.00,-2.00,6.00]
size(a) # The array is 1 row and 5 columns
b = zeros (1,5) # Notice how the lack of a semicolon results in printing out the statement below:
We can create a linear space (arithmetic progression) from point a to b with the help of the function $\textbf{linspace}$.
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:0.25:2
d = logspace(1, 4, 4)
e=[-2.18568893 0.74171085 0.95996753 1.13442837 1.62010761];
printf("%f \t", a); printf("\n"); printf("%f \t", e);
b = a+e; printf("%f", b); # Element-wise addition
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);
c = 2*a;
disp(a);
disp(c);
c = linspace(1., 4., 4);
disp(c)
d = logspace(1., 4., 4);
disp(d)
e = 10.^c;
disp(e)
disp(f);
f(3:5)
h = rand([1,10]) # Random array of size 1x10 with a uniform distribution between 0 and 1
k = h(2:8)
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.
fprintf("First element: %f \t Last element: %f\n", k(1), k(end))
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);
m = k<0 # For logical false Octave gives 0 as output
a=zeros(4,4)
b = rand(4,4); disp(b); #Random number generation
e = ones(4,4)
g = 2*b #Scalar multiplication
h=g*b #Proper matrix multiplication
k=g.*b #Element wise multiplication
p = k>1
k(k>1)