Octave Programming Tutorial/Vectors and matrices

< Octave Programming Tutorial

Creating vectors and matrices

Here is how we specify a row vector in Octave:

octave:1> x = [1, 3, 2]
x =

  1  3  2

Note that

To specify a column vector, we simply replace the commas with semicolons:

octave:2> x = [1; 3; 2]
x =

  1
  3
  2

From this you can see that we use a comma to go to the next column of a vector (or matrix) and a semicolon to go to the next row. So, to specify a matrix, type in the rows (separating each entry with a comma) and use a semicolon to go to the next row.

octave:3> A = [1, 1, 2; 3, 5, 8; 13, 21, 34]
A =

   1   1   2
   3   5   8
  13  21  34

Operators

You can use the standard operators to

matrices, vectors and scalars with one another. Note that the matrices need to have matching dimensions (inner dimensions in the case of multiplication) for these operators to work.

octave:4> A'
ans =

   1   3  13
   1   5  21
   2   8  34

(Note: this is actually the complex conjugate transpose operator, but for real matrices this is the same as the transpose. To compute the transpose of a complex matrix, use the dot transpose (.') operator.)

Element operations

When you have two matrices of the same size, you can perform element by element operations on them. For example, the following divides each element of A by the corresponding element in B:

octave:1> A = [1, 6, 3; 2, 7, 4]
A =

  1  6  3
  2  7  4

octave:2> B = [2, 7, 2; 7, 3, 9]
B =

  2  7  2
  7  3  9

octave:3> A ./ B
ans =

  0.50000  0.85714  1.50000
  0.28571  2.33333  0.44444

Note that you use the dot divide (./) operator to perform element by element division. There are similar operators for multiplication (.*) and exponentiation (.^).

Let's introduce a scalar for future use.

a = 5;

The dot divide operators can also be used together with scalars in the following manner.

C = a ./ B

returns a matrix, C where each entry is defined by

i.e. a is divided by each entry in B. Similarly

C = a .^ B

return a matrix with


Moreover, we can also calculate row-wise multiply or division:

a = [1 2 3; 4 5 6; 7 8 9]; b = [10; 11; 12]; c = b.*a; d = a./b;

Indexing

You can work with parts of matrices and vectors by indexing into them. You use a vector of integers to tell Octave which elements of a vector or matrix to use. For example, we create a vector

octave:1> x = [1.2, 5, 7.6, 3, 8]
x =

  1.2000  5.0000  7.6000  3.0000  8.0000

Now, to see the second element of x, type

octave:2> x(2)
ans = 5

You can also view a list of elements as follows.

octave:3> x([1, 3, 4])
ans =

  1.2000  7.6000  3.0000

This last command displays the 1st, 3rd and 4th elements of the vector x.

To select rows and columns from a matrix, we use the same principle. Let's define a matrix

octave:4> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A =

  1  2  3
  4  5  6
  7  8  9

and select the 1st and 3rd rows and 2nd and 3rd columns:

octave:5> A([1, 3], [2, 3])
ans =

  2  3
  8  9

The colon operator (:) can be used to select all rows or columns from a matrix. So, to select all the elements from the 2nd row, type

octave:6> A(2, :)
ans =

  4  5  6

You can also use : like this to select all Matrix elements:

octave:7> A(:,:)
ans =
 
  1   2   3
  4   5   6
  7   8   9

Ranges

We can also select a range of rows or columns from a matrix. We specify a range with

start:step:stop

You can actually type ranges at the Octave prompt to see what the results are. For example,

octave:3> 1:3:10
ans =

   1   4   7  10

The first number displayed was start, the second was start + step, the third, start + (2 * step). And the last number was less than or equal to stop.

Often, you simply want the step size to be 1. In this case, you can leave out the step parameter and type

octave:4> 1:10
ans =

   1   2   3   4   5   6   7   8   9  10

As you can see, the result of a range command is simply a vector of integers. We can now use this to index into a vector or matrix. To select the submatrix at the top left of A, use

octave:4> A(1:2, 1:2)
ans =

  1  2
  4  5

Finally, there is a keyword called end that can be used when indexing into a matrix or vector. It refers to the last element in the row or column. For example, to see the last column in a Matrix, you can use

octave:5> A(:,end)
ans =

  3
  6
  9

Functions

The following functions can be used to create and manipulate matrices.

Creating matrices

octave:16> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A =

  1  2  3
  4  5  6
  7  8  9

octave:17> x = diag(A)
ans =

  1
  5
  9

octave:18> diag(x)
ans =

  1  0  0
  0  5  0
  0  0  9
octave:186> linspace(2, 4, 2)
ans =

  2   4

octave:187> linspace(2, 4, 4)
ans =

  2.0000   2.6667   3.3333   4.0000

octave:188> linspace(2, 4, 6)
ans =

  2.0000   2.4000   2.8000   3.2000   3.6000   4.0000


octave:189> logspace(2, 4, 2)
ans =

    100   10000

octave:190> logspace(2, 4, 4)
ans =

  1.0000e+02   4.6416e+02   2.1544e+03   1.0000e+04

octave:191> logspace(2, 4, 5)
ans =

  1.0000e+02   3.1623e+02   1.0000e+03   3.1623e+03   1.0000e+04

Other matrices

There are some more functions for creating special matrices. These are

Use help to find out more about how to use these functions.

Changing matrices

octave:49> A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12] 
A =

   1   2   3   4
   5   6   7   8
   9  10  11  12

octave:50> fliplr(A)
ans =

   4   3   2   1
   8   7   6   5
  12  11  10   9
octave:51> flipud(A)
ans =

   9  10  11  12
   5   6   7   8
   1   2   3   4
octave:52> rot90(A)
ans =

   4   8  12
   3   7  11
   2   6  10
   1   5   9
octave:53> reshape(A, 2, 6)
ans =

   1   9   6   3  11   8
   5   2  10   7   4  12
octave:54> x = rand(1, 6)
x =

  0.25500  0.33525  0.26586  0.92658  0.68799  0.69682

octave:55> sort(x)
ans =

  0.25500  0.26586  0.33525  0.68799  0.69682  0.92658

Linear algebra

For a description of more operators and functions that can be used to manipulate vectors and matrices, find eigenvalues, etc., see the Linear algebra section.


Return to the Octave Programming Tutorial index

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.