0% found this document useful (0 votes)
2 views

Lecture9 Octave Data Types

The document provides an overview of data types and operations in Octave, focusing on arrays, variables, and mathematical operations. It explains the structure of arrays, including vectors and matrices, and details how to manipulate and perform calculations with them. Additionally, it covers logical and relational operators, as well as matrix multiplication and properties like identity matrices and matrix powers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture9 Octave Data Types

The document provides an overview of data types and operations in Octave, focusing on arrays, variables, and mathematical operations. It explains the structure of arrays, including vectors and matrices, and details how to manipulate and perform calculations with them. Additionally, it covers logical and relational operators, as well as matrix multiplication and properties like identity matrices and matrix powers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Octave:

Data types, operators


Arrays
 The fundamental unit of data in the Octave program is the array.
 An array is a collection of data values organised into rows and columns and
known by a single name.
 Octave treats scalars as arrays - they are arrays with one row and one column.
 Arrays can be classified as either vectors or matrices. A vector is a one-
dimensional array while a matrix is a multidimensional array.
 Total number of elements in an array is the sum of each element in the rows and
the columns.
 Example: » x=[1 2 3;4 5 6;7 8 9]
x= 1 2 3
4 5 6
7 8 9
 Note that the number of elements in every row of an array must be the same, and
the number of elements in every column must be the same.
 Individual data values within an array can be accessed by including the name of
the array followed by subscripts in parentheses that identify the row and column
of the particular value: >> x(2,1)
ans = 4
 If the array is a vector, then only one subscript is required.
© Prof Suvendi Rimer, UJ
Variables
 An Octave variable is a region of memory containing an array, which is known by
a user-specified name.
 Octave variables are automatically created when they are initialized.
 Variables can be initialized with arrays of data.
 These arrays are constructed using square brackets [] and semicommas (;).
 All of the elements of an array are listed in row order, with each row separated by
the semi-comma. Columns are separated by spaces or commas.
 Values in each row are listed from left to right, with the topmost row first and the
bottommost row last.
 Octave provides the colon operator to specify a whole series of values by
specifying the first value of the series, the stepping increment, and the last value
in the series.
 The general form of a colon operator is: first:incr:last
 Where first is the first value in the series, incr is the stepping increment, and last
is the last value in the series:
» t=1:2:10
t= 1 3 5 7 9

 Another useful method for creating a vector with series of values:


y = linspace(0,3,10)
© Prof Suvendi Rimer, UJ
Transpose operator
 Transpose operator (’) swaps row and columns of an array that it is applied to:
» g=1:4;
» h=[g' g']
h=
1 1
2 2
3 3
4 4

 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)

© Prof Suvendi Rimer, UJ


Multidimensional Arrays and Subarrays
 Octave allows us to create arrays with as many dimensions as necessary. These
arrays have one subscript for each dimension.
 A range of values can be selected using the colon operator (:).
 To return a column vector containing the second column in an 3 by 3 matrix,
substitute the row subscript by the colon and assign the column subscript the
value of the column:
» a=[1 2 3;4 5 6;7 8 9]
a=
1 2 3
4 5 6
7 8 9
» a(:,2)
ans =
2
5
8

 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

 Array-Array Maths: Mathematical operations between arrays are not as simple


as scalars and arrays.
 There are two types of operations possible when multiplying or dividing arrays.
 If element-by-element multiplication is required, then a ‘.’ Must be placed before
the operator. If matrix multiplication is required, then no ‘.’ must be placed before
the operator.
» a=[1 2 3];
» b=[1;2;3];
» a.*a
ans =
1 4 9
» a*a'
ans =
14
© Prof Suvendi Rimer, UJ » a*a
??? error: operator *: nonconformant arguments
Logical Operators
 Logical operators provide a way to combine or negate relational expressions.
 Logical operators applied directly to matrices
 For complex numbers, the following ordering is defined: z1 < z2 if and only if abs
(z1) < abs (z2) || (abs (z1) == abs (z2) && arg (z1) < arg (z2))
 This is consistent with the ordering used by max, min and sort, but is not
consistent with MATLAB, which only compares the real parts.
 String comparisons may also be performed with the strcmp function, not with the
operators listed below:

Logical Operator Description


& Element-by-element AND for arrays
| Element-by-element OR for arrays
~ Not
&& Scalar AND
|| Scalar OR

© Prof Suvendi Rimer, UJ


Relational operators
 Relational operators can be used to compare two arrays of the same size or an
array with a scalar
 x < y : True if x is less than y.
 x <= y : True if x is less than or equal to y.
 x == y : True if x is equal to y.
 x >= y : True if x is greater than or equal to y.
 x > y : True if x is greater than y.
 x != y : x ~= y : True if x is not equal to y.

 Operators having higher precedence are evaluated first.


 Operators having equal precedence are evaluated left to right.
 Octave has function isnan to determine NaNs
 Individual NaNs are not equal to each other
 Octave tests for empty arrays with the function isempty.

© Prof Suvendi Rimer, UJ


Operators and useful built-in functions for matrices

© Prof Suvendi Rimer, UJ


Array Manipulation
 Octave provides powerful ways to insert, extract and rearrange subsets of arrays.
Example: To change a specific element’s value:
>> a = [1 2 3; 4 5 6; 7 8 9]
a=
1 2 3
4 5 6
7 8 9
>> a(3,3) = 0
a=
1 2 3
4 5 6
7 8 0
 Similarly, to change all elements in a row or column use the “:” operator:
>> a(:,2) = 1
a=
1 1 3
4 1 6
7 1 0

© Prof Suvendi Rimer, UJ


Array Manipulation
 Subscripts refer to the row and column locations of elements of an array.
 Example: a(2,3) refers to the element in row 2 and column 3 of a.
 Array Size: In cases where the size of an array is unknown, Octave provides the
following utility functions
 Size: returns a row vector whose first element is the number of rows and the
second element is the number of columns.
 Length: returns the number of elements along the largest dimension.
 Numel: returns the total number of elements in an array.

© Prof Suvendi Rimer, UJ


Matrices in Octave
 A matrix is a 2-dimensional array of real or complex numbers.
 The following is a 3-by-2 rectangular matrix of random integers.
>> C = fix(10*rand(3,2))
C=
9 4
2 8
6 7
The statements
u = [3; 1; 4]
v = [2 0 -1]
s=7
produce a column vector, a row vector, and a scalar.
 Matrix elements are indexed in Row-Colum format.
 The first number indicates row and the second column.

© Prof Suvendi Rimer, UJ


Vector Products
 A row vector and a column vector of the same length can be multiplied in either
order. The result is either a scalar, the inner product, or a matrix, the outer
product.
>> u = [3; 1; 4]
u=
3
1
4
>> v = [2 0 -1]
v=
2 0 -1
>> u*v
ans =
6 0 -3
2 0 -1
8 0 -4
>> v*u
ans =
2

© Prof Suvendi Rimer, UJ


Transpose
 For real matrices, the transpose operation interchanges aij and aji.
 Octave uses the apostrophe (or single quote) to denote transpose.
 Transposition turns a row vector into a column vector.
 If x and y are both real column vectors, the product x*y is not defined, but the two
products x'*y and y'*x are the same scalar.
 This quantity is used so frequently, it has three different names: inner product,
scalar product, or dot product.

© Prof Suvendi Rimer, UJ


Mathematics in Octave: Addition and Subtraction
 Addition and subtraction of matrices same as for arrays, element-by-element.
 >> a = [1 2 3; 4 5 6; 7 8 9]
a=
1 2 3
4 5 6
7 8 9
>> b = [9 8 7; 6 5 4; 3 2 1]
b=
9 8 7
6 5 4
3 2 1
>> a+b
ans =
10 10 10
10 10 10
10 10 10

 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

 Matrix multiplied on the right by a column


vector and on the left by a row vector.
 Rectangular matrix multiplications must
satisfy the dimension compatibility
conditions.
 Inner matrix dimensions must agree.
Anything can be multiplied by a scalar.
>> r = [2 2 2]
r=
2 2 2 >> c = [2;2;2]
>> a*r c=
??? Error - Inner matrix dimensions must agree. 2
>> r*a 2
ans =
2
24 30 36
>> c*a
 ??? Error using ==> mtimes
 Inner matrix dimensions must agree.
>> a*c
ans =
12
30
© Prof Suvendi Rimer, UJ 48
Identity Matrix
 Generally accepted mathematical notation uses the capital letter I to denote
identity matrices, matrices of various sizes with ones on the main diagonal and
zeros elsewhere.
 These matrices have the property that AI = A and IA = A whenever the
dimensions are compatible.
 The function eye(m,n) returns an m-by-n rectangular identity matrix and eye(n)
returns an n-by-n square identity matrix.

© Prof Suvendi Rimer, UJ


Matrix Powers and Exponentials
 If A is a square matrix and p is a positive integer, then A^p multiplies A by itself p
times.
A=
1 2 3
4 5 6
7 8 9
>> p = 3
>> A^p
ans =
468 576 684
1062 1305 1548
1656 2034 2412

 Fractional powers, like A^(2/3), are also permitted.

© Prof Suvendi Rimer, UJ

You might also like