How can I append to a vector in Octave?

Aditya picture Aditya · Jun 13, 2014 · Viewed 57.8k times · Source

When ever I have to append to a vector I am doing this.

A = [2 3 4]
A = [A; 3 4 5]

I was wondering if there are any inbuilt functions for this or more elegant ways of doing this in Octave.

Answer

user3288829 picture user3288829 · Jun 13, 2014

The builtin functions are cat, vertcat, and horzcat, found on pages 380-381 of the Octave documentation (v 3.8). They are essentially equivalent to what you have though.

octave:5> A = [2 3 4];
octave:6> A = [A; 3 4 5]
A =

   2   3   4
   3   4   5

octave:7> B = [4 5 6];
octave:8> B = vertcat(B,[5 6 7])
B =

   4   5   6
   5   6   7

Another (again equivalent) way would be to directly use matrix indexing (see page 132)

octave:9> C = [6 7 8];
octave:10> C(end+1,:) = [7 8 9]
C =

   6   7   8
   7   8   9