Difference between append and x = [x, element]

前端 未结 2 1129
陌清茗
陌清茗 2020-12-07 03:41

I\'ve create an array X as X = [1 2 3 4 5] and I want to insert 0 at the end of it.

Is there any difference in using X =

相关标签:
2条回答
  • 2020-12-07 04:20

    append function is a part of Symbolic Math Toolbox. It's preferred to use [X, 0] as it is part of a core language and more likely to be understood.

    0 讨论(0)
  • 2020-12-07 04:32

    As explained in the other answer, append is part of a toolbox, and not available to everyone.

    The correct way to append to a matrix, however, is

    X(end+1) = 0;
    

    This is a whole lot more efficient than X=[X,0]. The difference is that this latter form creates a new array, and copies the original one into it. The other form simply appends to the matrix, which usually doesn't require reallocation. See here for an experiment that shows the difference (read the question and my answer for both parts of the experiment).

    0 讨论(0)
提交回复
热议问题