Computers

Octave Vectors and Matrices

Octave Vectors and Matrices

GNU Octave is a powerful open-source programming language widely used for numerical computing,
scientific calculations, data analysis, and mathematical modeling. One of the most important
concepts in Octave is working with vectors and matrices. Since Octave is designed
for numerical and matrix-based computation, understanding how to create, access, modify, and
manipulate vectors and matrices is essential for beginners and experienced programmers.

In this tutorial, you will learn about Octave vectors and matrices, including
vector creation, matrix creation, indexing, arithmetic operations, special matrices, and useful
built-in functions with practical examples.

What Are Vectors and Matrices in Octave?

In Octave, a vector is a one-dimensional array of values, while a
matrix is a two-dimensional array organized into rows and columns.
Octave treats these structures as fundamental data types for numerical computation.

A vector can be either a row vector or a column vector.
A matrix contains one or more rows and columns.

Type Description Example
Row Vector Values arranged horizontally [1 2 3 4]
Column Vector Values arranged vertically [1; 2; 3; 4]
Matrix Values arranged in rows and columns [1 2; 3 4]

Creating Vectors in Octave

Creating a Row Vector

A row vector can be created by placing elements inside square brackets and separating them
with spaces or commas.

a = [10 20 30 40]

The result is:


a =

   10   20   30   40
      

Commas can also be used:

a = [10, 20, 30, 40]

Creating a Column Vector

Use semicolons to separate elements when creating a column vector.

b = [10; 20; 30; 40]

The result is:


b =

   10
   20
   30
   40
      

Using the Colon Operator to Create Vectors

The colon operator (:) is one of the most useful features in Octave.
It allows you to quickly generate sequences of numbers.

Basic Syntax

start:stop

Example:

x = 1:5

This produces:

1  2  3  4  5

Using a Step Size

You can specify the increment by using:

start:step:stop

Example:

x = 2:2:10

The result is:

2  4  6  8  10

The colon operator is frequently used in loops, indexing, data generation, and numerical
calculations.

Using the linspace Function

The linspace function creates a vector containing a specified number of evenly
spaced values between two limits.

x = linspace(0, 10, 6)

The result is:

0  2  4  6  8  10

The syntax is:

linspace(start, stop, number_of_points)

This function is particularly useful when generating data points for mathematical functions
and plots.

Creating Matrices in Octave

Matrices are created using square brackets. Use spaces or commas to separate elements in a row
and semicolons to separate rows.


A = [1 2 3;
     4 5 6;
     7 8 9]
      

The resulting matrix is:


A =

   1   2   3
   4   5   6
   7   8   9
      

The matrix above contains 3 rows and 3 columns, so its size is
3 × 3.

Combining Vectors to Create Matrices

Combining Rows


row1 = [1 2 3];
row2 = [4 5 6];

A = [row1; row2]
      

This creates:


1  2  3
4  5  6
      

Combining Columns


a = [1; 2; 3];
b = [4; 5; 6];

A = [a b]
      

The result is:


1  4
2  5
3  6
      

Accessing Vector Elements

Octave uses one-based indexing. This means the first element is at index
1, not 0.


a = [10 20 30 40 50];

a(1)
a(3)
a(5)
      

The corresponding values are:


10
30
50
      

Accessing Multiple Elements

You can use a vector of indexes to select several elements.

a([1 3 5])

Result:

10  30  50

Accessing a Range

a(2:4)

Result:

20  30  40

Accessing Matrix Elements

Matrix elements are accessed using the syntax:

matrix(row, column)

Example:


A = [10 20 30;
     40 50 60;
     70 80 90];

A(2,3)
      

The result is:

60

Accessing an Entire Row

A(2, :)

Result:

40  50  60

Accessing an Entire Column

A(:, 2)

Result:


20
50
80
      

Modifying Vector Elements

Vector values can be changed by assigning a new value to a particular index.


a = [10 20 30 40];

a(2) = 99
      

The vector becomes:

10  99  30  40

Modifying Matrix Elements

Matrix elements can also be modified using row and column indexes.


A = [1 2 3;
     4 5 6;
     7 8 9];

A(2,2) = 50
      

The updated matrix is:


1   2   3
4  50   6
7   8   9
      

Matrix Dimensions in Octave

The size function returns the number of rows and columns in an array.


A = [1 2 3;
     4 5 6];

size(A)
      

The result is:


2   3
      

This means the matrix has 2 rows and 3 columns.

Number of Rows

rows(A)

Number of Columns

columns(A)

Number of Elements

numel(A)

Basic Vector Operations

Vector Addition


a = [1 2 3];
b = [4 5 6];

c = a + b
      

Result:

5  7  9

Vector Subtraction

c = b - a

Result:

3  3  3

Scalar Multiplication

c = 2 * a

Result:

2  4  6

Basic Matrix Operations

Matrix Addition


A = [1 2; 3 4];
B = [5 6; 7 8];

C = A + B
      

Result:


6   8
10  12
      

Matrix Subtraction

C = B - A

Result:


4  4
4  4
      

Matrix Multiplication

The * operator performs standard matrix multiplication.


A = [1 2; 3 4];
B = [5 6; 7 8];

C = A * B
      

Result:


19  22
43  50
      

Element-by-Element Operations

Octave provides operators that perform calculations independently on each element.
These operators use a dot before the arithmetic symbol.

Element-by-Element Multiplication


A = [1 2; 3 4];
B = [5 6; 7 8];

C = A .* B
      

Result:


5   12
21  32
      

Element-by-Element Division

C = A ./ B

Element-by-Element Power

C = A .^ 2

Understanding the difference between * and .* is especially important
when working with matrices and numerical data in Octave.

Transpose of a Vector or Matrix

The transpose operator ' changes rows into columns and columns into rows.

Transpose a Row Vector


a = [1 2 3];
b = a'
      

The result is:


1
2
3
      

Transpose a Matrix


A = [1 2 3;
     4 5 6];

B = A'
      

Result:


1  4
2  5
3  6
      

Special Matrices in Octave

Octave includes several built-in functions for generating commonly used matrices.

zeros()

Creates a matrix containing zeros.

zeros(3, 3)

ones()

Creates a matrix containing ones.

ones(2, 4)

eye()

Creates an identity matrix.

eye(3)

Example result:


1  0  0
0  1  0
0  0  1
      

rand()

Generates a matrix containing uniformly distributed random values.

rand(2, 3)

randi()

Generates random integer values.

randi(100, 3, 3)

Useful Functions for Vectors and Matrices

Function Purpose Example
size() Returns dimensions size(A)
length() Returns the largest dimension length(A)
numel() Returns total number of elements numel(A)
sum() Calculates the sum of elements sum(A)
mean() Calculates the average mean(A)
max() Returns the maximum value max(A)
min() Returns the minimum value min(A)
sort() Sorts values sort(A)
reshape() Changes the dimensions reshape(A, 2, 3)
diag() Creates or extracts diagonal elements diag(A)

Reshaping Matrices

The reshape() function changes the dimensions of an array without changing the
number of elements.


A = [1 2 3 4 5 6];

B = reshape(A, 2, 3)
      

The resulting matrix is:


1  3  5
2  4  6
      

The original and reshaped arrays must contain the same total number of elements.

Concatenating Matrices

Octave allows arrays to be combined horizontally or vertically.

Horizontal Concatenation


A = [1 2];
B = [3 4];

C = [A B]
      

Result:

1  2  3  4

Vertical Concatenation


C = [A; B]
      

Result:


1  2
3  4
      

Deleting Elements from Vectors and Matrices

Delete a Vector Element


a = [10 20 30 40];

a(2) = []
      

The result is:

10  30  40

Delete a Matrix Column


A = [1 2 3;
     4 5 6;
     7 8 9];

A(:,2) = []
      

The resulting matrix is:


1  3
4  6
7  9
      

Logical Indexing

Logical indexing is a powerful feature for selecting elements based on conditions.


a = [10 15 20 25 30];

a(a > 20)
      

The result is:

25  30

Logical indexing is commonly used for filtering numerical data, processing datasets,
and implementing conditional calculations.

Example: Performing Calculations on a Vector


marks = [75 82 91 68 88];

total = sum(marks)
average = mean(marks)
highest = max(marks)
lowest = min(marks)
      

This example demonstrates how Octave can quickly perform statistical calculations on a vector.

Example: Working with a Matrix


A = [10 20 30;
     40 50 60;
     70 80 90];

total = sum(A(:))
rowTotals = sum(A, 2)
columnTotals = sum(A, 1)
      

Here, A(:) converts the matrix elements into a single column vector, allowing
the total of all elements to be calculated easily.

Vectors and Matrices in Scientific Computing

Vectors and matrices are central to many applications of Octave. They are commonly used in
linear algebra, numerical analysis, engineering simulations, statistics, machine learning,
signal processing, image processing, optimization, and scientific research.

For example, a system of linear equations can be represented using matrices and vectors:

A * x = b

Octave can then be used to solve the system efficiently using built-in numerical functions.

Common Mistakes When Working with Octave Arrays

Using the Wrong Index

Remember that Octave indexing starts at 1.

Confusing Matrix Multiplication and Element-Wise Multiplication

Use * for matrix multiplication and .* for element-by-element
multiplication.

Mixing Incompatible Dimensions

Matrix operations require compatible dimensions. Always check the size of vectors and matrices
before performing operations.

Forgetting Semicolons

A semicolon at the end of a statement suppresses output in the Octave command window.

A = [1 2; 3 4];

Octave Vectors vs Matrices

Feature Vector Matrix
Dimensions Usually one-dimensional Two-dimensional or higher-dimensional arrays can be used
Structure Row or column Rows and columns
Example [1 2 3] [1 2; 3 4]
Common Uses Lists, data points, coefficients Linear algebra, transformations, datasets

Best Practices for Working with Vectors and Matrices in Octave

Use meaningful variable names, check array dimensions before performing operations, and take
advantage of Octave’s vectorized operations instead of unnecessarily using loops.

Built-in functions such as size(), length(), reshape(),
sum(), and mean() can make numerical programs simpler and more readable.

Conclusion

Octave vectors and matrices form the foundation of numerical programming in
GNU Octave. By learning how to create vectors and matrices, access individual elements,
modify data, perform mathematical operations, reshape arrays, and use built-in functions,
you can solve a wide range of numerical and scientific computing problems.

Mastering vectors and matrices is an important step toward learning advanced Octave topics
such as linear algebra, plotting, data analysis, numerical methods, optimization, and
scientific simulations.

Frequently Asked Questions About Octave Vectors and Matrices

What is a vector in Octave?

A vector is an array of values arranged as a row or column. For example,
[1 2 3 4] is a row vector.

How do you create a matrix in Octave?

Use square brackets, spaces or commas for columns, and semicolons for rows. For example,
[1 2; 3 4] creates a 2 × 2 matrix.

What does the colon operator do in Octave?

The colon operator creates sequences and is also widely used for indexing rows and columns.

What is the difference between * and .* in Octave?

The * operator performs matrix multiplication, while .* performs
multiplication element by element.

How does indexing work in Octave?

Octave uses one-based indexing, so the first element of an array is accessed using index
1.

Octave Tutorials

GNU Octave Tutorials on this website can be found at:

For more information on Octave, visit the official website: