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.

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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);

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

ColumnVector = [1; 2; 3; 4; 5];
disp(ColumnVector);

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

Vector = [10 20 30 40 50];
for i = 1 : 5
    printf("Element at Vector(%d) is %d\n", i, Vector(i));
endfor

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.