41

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.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Aditya
  • 1,240
  • 2
  • 14
  • 38

2 Answers2

36

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
user3288829
  • 1,266
  • 15
  • 26
8

I think that the most efficient is to use this built in function that you have posted in the question(I'm relying on other experts in octave i did not check it completely; The standard is that matrix operations are generally faster than iterative ones, I don't know what the inner mechanism that allows this to be enabled yet). Because a vector is a type of matrice, this solution will work for concatinating vectors (of any type) too:

vector = [vector ; value]
sivi
  • 10,654
  • 2
  • 52
  • 51