Vectors in Octave GNU
Vector in Octave is similar to the array in every aspect except the vector size is dynamic, which means its size can be increased however array does not allow such type of increment in its size.
Types of Vectors
- Row Vector
- Column Vector
Row vectors are created by enclosing the set of elements in square brackets, using space or comma to delimit the elements.
% using space as the delimiter RowVector1 = [1 2 3 4 5]; disp(RowVector1); % using comma as the delimiter RowVector2 = [1, 2, 3, 4, 5]; disp(RowVector2); |
Output :
1 2 3 4 5 1 2 3 4 5
Column vectors are created by enclosing the set of elements in square brackets, using a semicolon to delimit the elements.
ColumnVector = [1; 2; 3; 4; 5]; disp(ColumnVector); |
Output :
1 2 3 4 5
Accessing Elements in a Vector in Octave GNU : A vector element is accessed using the index of that element. The indexing starts from 1(not from 0). Language like C++, Java uses index from 0 to size-1, whereas in OCTAVE GNU index starts from 1 and ends at size; Also C++, java uses [] (Square Bracket) to access elements using index but in OCTAVE GNU parenthesis are used to access elements using the index.
Vector = [10 20 30 40 50]; for i = 1 : 5 printf( "Element at Vector(%d) is %d\n" , i, Vector(i)); endfor |
Output :
Element at Vector(1) is 10 Element at Vector(2) is 20 Element at Vector(3) is 30 Element at Vector(4) is 40 Element at Vector(5) is 50