Defining matrices and their transposes on GNU Octave

Hey scientist! How is it going?
Ready to learn how to use matrices on GNU Octave? In this post we will define vectors and matrices, and also their transposes. Let’s do it!

First of all, open your Octave! … Didn’t you installed it yet? Check here how to do it.

We will type all commands on Octave’s “Command Window”. It shows a prompt (two “bigger or equal” signs), where Octave says “I’m here and ready to rock!”.

A matrix is a vector with two or more dimensions. Let’s define first a little vector, an element with one dimension. This is easy: we put all elements belonging to it between brackets. We separate the elements using spaces. Check it out:

>> ve1 = [1 0 3 pi]

ve1 =
   1.00000   0.00000   3.00000   3.14159

Here the variable ve1 receives the elements 1, 0, 3 and pi. Note that our vector contains one row and four columns. Because it contains one row only, we call it a column vector.

In order to show to Octave where do we want a new row, we type a semicolon between the elements:

>> ve2 = [2; e; 3; 1]

ve2 =

Here, ve2 receives the elements 2, e, 3 and 1. Note that this time our vector contains one column and four rows; in this case we call it a row vector.

Now let’s define real matrices! We use brackets to define them, and semicolons to indicate where a new row begin. Easy, right? Check how we can do it:

>> matr1 = [1 0; 3 2]

matr1 =
   1   0
   3   2

In this example the variable matr1 receives two elements on the first row (1 and 0) and two elements on the second row (3 and 2). We call this matrix a square matrix, because it has the same number of rows and columns. Another loving way to call it is matrix of order 2.
One more example:

>> matr2 = [1 0 1; 2 1 1]

matr2 =
   1   0   1
   2   1   1

Finally, we define an elementary operation: the transposition. Using it we exchange rows by columns on a vector/matrix! To do that we can use the function transpose() or the operator '.

Using the previous examples:

>> transpose(vetor1)

ans =

>> vetor2'

ans =
   2.0000   2.7183   3.0000   1.0000

>> transpose(matr1)

ans =
   1   3
   0   2

>> matr2'

ans =
   1   2
   0   1
   1   1

That’s it scientist! Did you like this introduction to vectors and matrices on Octave?

Next week we continue the post series about Octave, with operations using matrices!
Gigaregards! See you next time!

Like this? Please comment and share with your friends!
Want to download Programando Ciência codes? Go to our GitHub!
Make a donation for Programando Ciência!
Like us also on Facebook:
I’m on Twitter! Follow me if you can! @alexdesiqueira

One thought on “Defining matrices and their transposes on GNU Octave

  1. Olá, tudo bom? Então, estou tendo algumas dificuldades de utilizar o octave pelo prompt, uma delas é a de como abir um script em branco para criar funções, etc… Pelo Matlab ou pelo octave –force-gui eu escrevia “edit” na janela de comandos e ele abria, mas no prompt não está indo. Você poderia me ajudar?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s