简体   繁体   English

如何在Octave中附加向量?

[英]How can I append to a vector in Octave?

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. 我想知道是否有任何内置函数用于在Octave中执行此操作或更优雅的方法。

The builtin functions are cat, vertcat, and horzcat, found on pages 380-381 of the Octave documentation (v 3.8). 内置函数是cat,vertcat和horzcat,可在Octave文档 (第3.8版)的第380-381页找到。 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) 另一种(同样的)方法是直接使用矩阵索引(参见第132页)

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

   6   7   8
   7   8   9

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM