Site icon TestingDocs.com

Create a New Matrix using Octave

Overview

In this tutorial, we will learn the steps to Create a New Matrix using GNU Octave.

Create a New Matrix

We can create a new matrix or vector with square brackets. We can specify the matrix elements between []. We can use a comma or a space to separate entries in a row, and a semicolon( ; ) or carriage return to separate the rows.
We can store the matrix in a variable so that we can refer to it later in the script.

Example

To create a new matrix with 2 rows and 3 columns.

We can end the command with a semicolon(;) to tell Octave not to print the result of the command or the variable.

In-built Functions

Octave also provides some in-built functions to create vectors. The rand(row, column) function creates a matrix with random values.

For example, to create a matric with 3 rows and 4 columns with random elements, we can use the following command:

>> rand(3,4) 

By default, each element in the matrix will be set to a value between 0 and 1.

The other most used functions to create matrices are:

For example, the function zeros(2) creates a matrix with all elements as zero with two rows and two columns.

>> a = zeros(2)

a =

0    0

0    0

 

The function ones(2) creates a matrix with all elements as one with two rows and two columns.

Octave Tutorials

GNU Octave Tutorials on this website can be found at:

https://www.testingdocs.com/octave-tutorial/

For more information on Octave, visit the official website:

https://www.gnu.org/software/octave/index

Exit mobile version