Lecture9 Octave Data Types
Lecture9 Octave Data Types
Arrays can be initialized using built-in Octave functions. Some of the functions:
Random numbers
rand function is a
pseudo-random number
generator that generates
a uniformly distributed
random number between
1 and 0: rand(r) or rand
(r,c)
Array Concatenation
Octave allows us to combine multiple arrays into single array.
Example: a = magic(3) ; b = ones(3,2)
x = [a b]
x = [a b’]
© Prof Suvendi Rimer, UJ
Scalar-Array and Array-Array Mathematics
Scalar Maths: Addition, subtraction, multiplication and division by a scalar simply
apply the operation to all elements of the array. Example: >> g = [1 2 3 4; 5 6 7 8]
g=
1 2 3 4
5 6 7 8
>> g-2
ans =
-1 0 1 2
3 4 5 6
Addition and subtraction require both matrices to have the same dimension, or
one of them to be a scalar.
If the dimensions are incompatible, an error results.
>> c = [1 2 3; 1 1 1]
c=
1 2 3
1 1 1
>> a - c
© Prof Suvendi Rimer, UJ operator -: nonconformant arguments
error:
Matrix Multiplication
Matrix multiplication - composition of the underlying linear transformations and
allows compact representation of systems of simultaneous linear equations.
The matrix product C = AB is defined when the column dimension of A is equal to
the row dimension of B, or when one of them is a scalar.
If A is m-by-p and B is p-by-n, their product C is m-by-n.
Octave uses a single asterisk to denote matrix multiplication.
Examples: illustrate matrix multiplication is not commutative; AB is usually not
equal to BA.
a= 1 2 3
4 5 6
7 8 9
>> b = 9 8 7
6 5 4
3 2 1
>> a*b
ans =
30 24 18
84 69 54
138 114 90
>> b*a
ans =
90 114 138
54 69 84
© Prof Suvendi Rimer, UJ
18 24 30
Matrix Multiplication