Computers

Create a New Matrix

Create a New Matrix

In this tutorial, we will learn how to Create a New Matrix using GNU Octave. 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 to refer to it later in the script.

Example

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

Create a New Matrix Octave

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.

 

Rand Octave Function

The other most used functions to create matrices are:

  • zeros
  • ones

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: