lettura simple

Array in octave

In this lesson I'll explain how to create an array in Octave

What is an array? It is an ordered set of data. If you have multiple values of the same type to store, instead of using multiple variables you can use a single array variable containing multiple elements. In each element you register a different value. An array is similar to a vector or a matrix in mathematics.

I'll give you a practical example.

Write the name of the array variable followed by the equal symbol.

Then write the elements of the vector in the square brackets, separating them from each other with a comma.

>> vector = [1, 2, 3, 4, 5]

The result is a vector with five elements in line.

vector =
1 2 3 4 5

You can also indicate elements by separating them only by a space

The final outcome is the same

>> vector = [1 2 3 4 5]
vector =
1 2 3 4 5

If you separate the elements with the semicolon symbol, you get a vector with five elements but arranged in a column

>> vector = [1; 2; 3; 4; 5]
vector =
1
2
3
4
5

Note. The difference between row and column vector is very important in mathematical calculation. For example, you can add two row vectors or two column vectors with the same elements. You cannot sum a row vector with a column vector.

You can also create an array with alphanumeric elements, write the elements in quotes.

>> letters = ['A', 'B', 'C', 'D']

If the elements are strings it is sometimes useful to put the elements in curly brackets.

>> city = [{"Rome", "Paris", "London", "Madrid"}]

The result is a row vector with four alphanumeric elements

city = Rome Paris London Madrid




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin